Re: [powermock] UnfinishedStubbingException when mocking multiple static methods within a test method

1,451 views
Skip to first unread message
Message has been deleted

Johan Haleby

unread,
Aug 4, 2014, 2:01:27 AM8/4/14
to powe...@googlegroups.com
Hi, 

In your example it doesn't seem like you need to do spying since you're mocking out all methods of the Utils class. Is this really what you want? In that case you could just use a normal mock. You can find some examples of spying with PowerMockito here.

Regards,
/Johan


On Wed, Jul 30, 2014 at 1:33 AM, Hristo Oskov <hristo...@gmail.com> wrote:
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.

Reply all
Reply to author
Forward
0 new messages