How to verify static void method has been called with power mockito

前端 未结 2 1272
清酒与你
清酒与你 2020-12-02 13:14

I am using the following.

Powermock-mockito 1.5.12
Mockito 1.95
junit 4.11

Here is my utils class

public void InternalUtils         


        
2条回答
  •  醉酒成梦
    2020-12-02 13:27

    If you are mocking the behavior (with something like doNothing()) there should really be no need to call to verify*(). That said, here's my stab at re-writing your test method:

    @PrepareForTest({InternalUtils.class})
    @RunWith(PowerMockRunner.class)
    public class InternalServiceTest { //Note the renaming of the test class.
       public void testProcessOrder() {
            //Variables
            InternalService is = new InternalService();
            Order order = mock(Order.class);
    
            //Mock Behavior
            when(order.isSuccessful()).thenReturn(true);
            mockStatic(Internalutils.class);
            doNothing().when(InternalUtils.class); //This is the preferred way
                                                   //to mock static void methods.
            InternalUtils.sendEmail(anyString(), anyString(), anyString(), anyString());
    
            //Execute
            is.processOrder(order);            
    
            //Verify
            verifyStatic(InternalUtils.class); //Similar to how you mock static methods
                                               //this is how you verify them.
            InternalUtils.sendEmail(anyString(), anyString(), anyString(), anyString());
       }
    }
    

    I grouped into four sections to better highlight what is going on:

    1. Variables

    I choose to declare any instance variables / method arguments / mock collaborators here. If it is something used in multiple tests, consider making it an instance variable of the test class.

    2. Mock Behavior

    This is where you define the behavior of all of your mocks. You're setting up return values and expectations here, prior to executing the code under test. Generally speaking, if you set the mock behavior here you wouldn't need to verify the behavior later.

    3. Execute

    Nothing fancy here; this just kicks off the code being tested. I like to give it its own section to call attention to it.

    4. Verify

    This is when you call any method starting with verify or assert. After the test is over, you check that the things you wanted to have happen actually did happen. That is the biggest mistake I see with your test method; you attempted to verify the method call before it was ever given a chance to run. Second to that is you never specified which static method you wanted to verify.

    Additional Notes

    This is mostly personal preference on my part. There is a certain order you need to do things in but within each grouping there is a little wiggle room. This helps me quickly separate out what is happening where.

    I also highly recommend going through the examples at the following sites as they are very robust and can help with the majority of the cases you'll need:

    • https://github.com/powermock/powermock/wiki/Mockito (PowerMock Overview / Examples)
    • http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html (Mockito Overview / Examples)

提交回复
热议问题