vendredi 3 avril 2015

Java: How to get full failure trace when invoking method with reflection?

for unit test purposes and template method pattern, I need to call some methods dynamically, like this:



//Unit test method call to wrap into a global test logic
Method m = myobject.getClass().getMethod("run");
m.invoke(...);


It's works, but when method fails (assertion fails or runtime exceptions), I can't have the full failure trace. For example, if you have a sequence call: method1() -> method2() -> method3() and the method3 fails, you have the following failure trace:



java.lang.<SomeError>:
at myexample.MyClass1.method3()
at myexample.MyClass1.method2()
at myexample.MyClass1.method1()
at myexample.Entrypoint.someEntrypoint()


It's the normal display when calling "method1()" explicitely in your code. But if you do: myobject.getClass().getMethod("method1").invoke(). If method3 fails, you have the following failure trace:



java.lang.<SomeError>:
at myexample.Entrypoint.someEntrypoint() // calling m.invoke()


So, when calling method1 with reflection (m.invoke()), the failure trace stops at invoke() instead of invoke() -> method2() -> method3(). There is a way to get the full trace ? Thanks.


NB: Thread.currentThread().getStackTrace() doesn't solve the problem and provides a similar output.






Aucun commentaire:

Enregistrer un commentaire