Java – mockito: when method A.A is called, then execute B.B
I'm using mockito for JUnit testing
class A{ public A(){} public final String a(String x){ return "A.a: " + x; } }
And I want to replace method call A.A with another method call, which has the same parameters and the same type of return value As you can see, it is impossible to override method a by extending the class to final. Now I am another method B.B of class B:
class B{ public B(){} public String b(String x){ return "B.b: " + x; } }
Now I want to make sure that every time I call A.A from code, I use the return value of B.B Is it possible to achieve this goal through mockito (like mockito. When (A.A (x)), thenreturn (B.B (x));), But using the same parameter x without knowing the value of X?
Any help will be greatly appreciated. Thank you in advance!
Solution
According to mockito limits, it is impossible to cover the final method of mockito generated simulation However, you can use powermock to crack the code at run time so that mockito can do its work