如何使用power mockito调用静态void方法

Chu*_*ang 41 java static-methods unit-testing mockito

我使用以下内容.

Powermock-mockito 1.5.12
Mockito 1.95
junit 4.11
Run Code Online (Sandbox Code Playgroud)

这是我的utils类

public void InternalUtils {
    public static void sendEmail(String from, String[] to, String msg, String body) {
    }
}
Run Code Online (Sandbox Code Playgroud)

这是被测试班级的要点:

public class InternalService {
       public void processOrder(Order order) {
           if (order.isSuccessful()) {
               InternalUtils.sendEmail(...);
           }
       }
}
Run Code Online (Sandbox Code Playgroud)

以下是测试:

@PrepareForTest({InternalUtils.class})
@RunWith(PowerMockRunner.class)
public class InternalService {
   public void verifyEmailSend() {
        mockStatic(Internalutils.class);
        doNothing().when(InternalUtils, "sendEmail", anyString(), any(String.class), anyString(), anyString());
        Order order = mock(Order.class);
        when(order.isSuccessful()).thenReturn(true);
        InternalService is = new InternalService();

        verifyStatic(times(1));
        is.processOrder(order);
   }
}
Run Code Online (Sandbox Code Playgroud)

上述测试失败.给出的验证模式为none,但根据代码,如果订单成功,则必须发送电子邮件.

Mat*_*man 54

如果你在嘲笑行为(有类似的东西doNothing()),那么真的没有必要打电话给verify*().也就是说,这是我重写你的测试方法:

@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());
   }
}
Run Code Online (Sandbox Code Playgroud)

我分为四个部分,以更好地突出显示正在发生的事情:

1.变量

我选择在这里声明任何实例变量/方法参数/模拟协作者.如果它是多个测试中使用的东西,请考虑将其作为测试类的实例变量.

2.模拟行为

您可以在此定义所有模拟的行为.在执行测试代码之前,您在此处设置了返回值和期望值.一般来说,如果您在此处设置模拟行为,则无需稍后验证该行为.

3.执行

没什么好看的; 这只是开始测试的代码.我喜欢给它自己的部分来引起注意.

4.验证

这是当您调用以verify或开头的任何方法时assert.测试结束后,您会检查您想要发生的事情是否确实发生过.这是我用你的测试方法看到的最大错误; 您尝试在有机会运行之前验证方法调用.其次要被你从来没有指定哪个你想验证静态方法.

补充说明

这主要是我个人的偏好.你需要做一些事情,但在每个分组中都有一个小小的摆动空间.这有助于我快速分离出发生在哪里的事情.

我还强烈建议您浏览以下网站上的示例,因为它们非常强大,可以帮助解决您需要的大多数情况:

  • verifyStatic(次(1)); == verifyStatic(); (6认同)
  • 我试图验证返回void的静态方法只被调用一次(带有一些特定的参数).我不得不从上面的代码中删除doNothing语句,以使其工作. (2认同)

Arp*_*pit 16

你上面的答案被广泛接受并且有详细记录,我发现在这里发布我的答案的一些理由: -

    doNothing().when(InternalUtils.class); //This is the preferred way
                                           //to mock static void methods.
    InternalUtils.sendEmail(anyString(), anyString(), anyString(), anyString());
Run Code Online (Sandbox Code Playgroud)

在这里,我不明白为什么我们自己调用InternalUtils.sendEmail.我将在我的代码中解释为什么我们不需要这样做.

mockStatic(Internalutils.class);
Run Code Online (Sandbox Code Playgroud)

所以,我们嘲笑了这个很好的课程.现在,让我们看看我们如何验证sendEmail(/..../)方法.

@PrepareForTest({InternalService.InternalUtils.class})
@RunWith(PowerMockRunner.class)
public class InternalServiceTest {

    @Mock
    private InternalService.Order order;

    private InternalService internalService;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        internalService = new InternalService();
    }

    @Test
    public void processOrder() throws Exception {

        Mockito.when(order.isSuccessful()).thenReturn(true);
        PowerMockito.mockStatic(InternalService.InternalUtils.class);

        internalService.processOrder(order);

        PowerMockito.verifyStatic(times(1));
        InternalService.InternalUtils.sendEmail(anyString(), any(String[].class), anyString(), anyString());
    }

}
Run Code Online (Sandbox Code Playgroud)

这两行是神奇的地方,第一行告诉PowerMockito框架它需要验证静态模拟的类.但它需要验证哪种方法?第二行告诉它需要验证哪种方法.

PowerMockito.verifyStatic(times(1));
InternalService.InternalUtils.sendEmail(anyString(), any(String[].class), anyString(), anyString());
Run Code Online (Sandbox Code Playgroud)

这是我班的代码,sendEmail api两次.

public class InternalService {

    public void processOrder(Order order) {
        if (order.isSuccessful()) {
            InternalUtils.sendEmail("", new String[1], "", "");
            InternalUtils.sendEmail("", new String[1], "", "");
        }
    }

    public static class InternalUtils{

        public static void sendEmail(String from, String[]  to, String msg, String body){

        }

    }

    public class Order{

        public boolean isSuccessful(){
            return true;
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

因为它是两次调用你只需要更改验证(次(2))......就是这样.