Here is an example class.
public class Example {
private String method1() {
return "Hello";
}
private String method2() {
//Makes an external API call which I can't make
return "World";
}
public String methodBeingTested() {
String m1 = method1();
String m2 = method2();
return m2
}
}
My intention is that the unit test passes through method1()
for code coverage purposes, but does NOT pass through method2()
because it can't be called!
Here is what I've tried:
Creating a Mock of the
Example
class and doing a.when(exampleMock.methodBeingTested()).thenReturn(createdString)
but that doesn't end up making the call at all (Shown by code coverage)Not creating the Mock but passing an actual instance,
.when(actualExampleInstance.methodBeingTested()).thenReturn(createdString)
in which case the functionmethod2()
does get called and an error is thrown because I still can't actually make the call to the external API but the code needs to be there..