--I'm using PowerMock in conjunction with Mockito and TestNG to test a method which makes multiple calls to static utility methods. For the test, I don't care about actually executing the static utility methods... I just want to stub those calls and return mock data.However, I'm running into a problem with the following exception:Caused by: org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)
at org.powermock.api.mockito.PowerMockito.doReturn(PowerMockito.java:789)
...Here's my setup...The test class:
@PrepareForTest(Utils.class)
public class TestFooBar {
@Test
public void testFooBar() {
try {
PowerMockito.spy(Utils.class);
PowerMockito.doReturn(100).when(Utils.class, "helper1");
PowerMockito.doReturn(200).when(Utils.class, "helper2");
PowerMockito.doReturn(300).when(Utils.class, "helper3");
} catch (Exception e) {
Assert.fail("caught exception when trying to mock static method", e);
}
DataModel dataModel = FooBar.doWork();
// assert the DataModel is correct
}
}The class containing the method being tested:public class FooBar {
public static DataModel doWork() {
...
int result1 = Utils.helper1();
...
int result2 = Utils.helper2();
...
int result3 = Utils.helper3();
// returns a DataModel containing the results
}
}
The utility class with the helper methods (each helper method may call other methods, make network requests, etc...):public class Utils {
public static int helper1() {
// do stuff, call other methods from other classes
}
public static int helper2() {
// do stuff, call other methods from other classes
}
public static int helper3() {
// do stuff, call other methods from other classes
}
}After debugging, it looks the flow goes into the catch() block after trying to execute the 2nd PowerMockito.doReturn() in the try block. What am I doing wrong here?
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.