如何验证已使用 power mockito 调用了静态 void 方法 [英] How to verify static void method has been called with power mockito

查看:99
本文介绍了如何验证已使用 power mockito 调用了静态 void 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下内容.

Powermock-mockito 1.5.12
Mockito 1.95
junit 4.11

这是我的工具类

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

这里是被测类的要点:

public class InternalService {
       public void processOrder(Order order) {
           if (order.isSuccessful()) {
               InternalUtils.sendEmail(...);
           }
       }
}

这里是测试:

@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);
   }
}

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

The above test fails. The verification mode given is none, but according to the code, if order is successful than email must be send.

推荐答案

如果你在嘲笑行为(使用类似 doNothing() 的东西),真的不需要调用 验证*().也就是说,这是我重写您的测试方法的尝试:

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:

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

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.

这是您定义所有模拟行为的地方.在执行被测代码之前,您在此处设置返回值和期望值.一般来说,如果你在这里设置了模拟行为,你以后就不需要验证行为了.

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.

这里没什么好看的;这只是启动正在测试的代码.我喜欢给它自己的部分来引起人们的注意.

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

这是当您调用以 verifyassert 开头的任何方法时.测试结束后,您检查您希望发生的事情是否确实发生了.这是我在您的测试方法中看到的最大错误;您试图在方法调用有机会运行之前对其进行验证.其次是您从未指定哪个要验证的静态方法.

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.

这主要是我的个人喜好.你需要按照一定的顺序做事,但在每个分组内都有一点回旋余地.这有助于我快速区分发生的事情.

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)

这篇关于如何验证已使用 power mockito 调用了静态 void 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆