Mock使用场景

  (1)创建所需的DB数据可能需要很长时间,如:调用别的接口,模拟很多数据,确保发布版本接口可用

  (2)调用第三方API接口,测试很慢,

  (3)编写满足所有外部依赖的测试可能很复杂,复杂到不值得编写,Mock模拟内部或外部依赖可以帮助我们解决这些问题

  (4)隔离当前方法使用的的所有依赖,让我们更加专注于单个单元,忽略其调用的代码的内部工作原理

Mock的好处:

  (1)Mock可以用来解除测试对象对外部服务的依赖(比如数据库,第三方接口等),使得测试用例可以独立运行。不管是传统的单体应用,还是现在流行的微服务,这点都特别重要,因为任何外部依赖的存在都会极大的限制测试用例的可迁移性和稳定性。

  (2)Mock的第二个好处是替换外部服务调用,提升测试用例的运行速度。任何外部服务调用至少是跨进程级别的消耗,甚至是跨系统、跨网络的消耗,而Mock可以把消耗降低到进程内。比如原来一次秒级的网络请求,通过Mock可以降至毫秒级,整整3个数量级的差别。

  (3)Mock的第三个好处是提升测试效率。这里说的测试效率有两层含义。第一层含义是单位时间运行的测试用例数,这是运行速度提升带来的直接好处。而第二层含义是一个测试人员单位时间创建的测试用例数。

  常用的Mock工具有jMock 、EasyMock 、Mockito等,但都有一个共同的缺点:不能mock静态、final、私有方法等。而PowerMock能够完美的弥补其他Mock工具的不足,如果需要mock静态方法。需要引入PowerMock。

  Springboot的  spring-boot-starter-test 已包含了Mockito,可以在Springboot项目里面直接使用。

PowerMock使用技巧

  ① @RunWith(PowerMockRunner.class)和@PrepareForTest(PowerMockClass.Class)这两个注解一定要加,否则PowerMock无效。@PrepareForTest中需要添加被测试的类,以及被测方法中需要mock的static方法所属的类。
  如果有多个类要添加,则格式为:
  @PrepareForTest({Class1.Calss,Class2.Class}).

  ② PowerMock各方法的语法(一般方法与Mockito用法一致):

  (1) void静态:PowerMockito.mockStatic(xxx.Class);
  PowerMockito.doNothing().when(mock,”methodName”,arg1,arg2);

  (2)有返回值静态:PowerMockito.mockStatic(xxx.Class);
  PowerMockito.when(mock.method()).thenReturn(value);

  (3)私有有返回值静态:PowerMockito.mock(xxx.Class);
  PowerMockito.when(mock,”methodName”,arg1,arg2).thenReturn(value);

  (4)构造函数:PowerMockito.mock(xxx.Class);
  PowerMockito.whenNew(xxx.Class).withAnyArguments().thenReturn();

测试示例

@RequestMapping(value = "", method = RequestMethod.POST)
public ReturnMsg crossDomainSetCookie(HttpServletRequest request, HttpServletResponse response,
                                      String name, String value)
{
    Cookie[] cookies = request.getCookies();
    for (Cookie c : cookies)
    {
        System.out.println("CookieName: " + c.getName() + "  CookieValue: " + c.getValue());
    }

    HttpSession session = request.getSession();
    Enumeration<String> attributeNames = session.getAttributeNames();
    while (attributeNames.hasMoreElements())
    {
        String element = attributeNames.nextElement();
        System.out.println(element);
    }
    try
    {
        String authType = request.getAuthType();
        System.out.println("auth type is:" + authType);
    }
    catch (Exception e)
    {
        System.out.println("exception message:" + e.getMessage());
    }

    int status = response.getStatus();
    System.out.println("the response status is:" + status);
    return ReturnMsg.generatorSuccessMsg("success");
}

测试代码

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentControllerTest
{
    /**
     * 自动注入
     */
    @Autowired
    private StudentController studentController;

    @Test
    public void crossDomainSetCookie()
    {
        HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
        //Mock带参方法返回值
        Mockito.when(request.getHeader("Origin")).thenReturn("127.0.0.1");
        Cookie[] cookies = {new Cookie("Login", "true")};
        //设置无参方法返回值
        Mockito.when(request.getCookies()).thenReturn(cookies);
        //Mock对象实例
        HttpSession session = Mockito.mock(HttpSession.class);
        Enumeration<String> en = Mockito.mock(Enumeration.class);
        //Mock集合对象返回多个值,第一次返回值true,第二次返回值ture,第三次及以后返回false
        Mockito.when(en.hasMoreElements()).thenReturn(true).thenReturn(true).thenReturn(false);
        Mockito.when(en.nextElement()).thenReturn("login").thenReturn("loginFlag");
        Mockito.when(session.getAttributeNames()).thenReturn(en);
        Mockito.when(request.getSession()).thenReturn(session);
        //Mock方法调用时抛出异常
        doThrow(new IllegalArgumentException("nothing auth type!")).when(request).getAuthType();

        HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
        Mockito.when(response.getStatus()).thenReturn(1);
        String value = "e10adc3949ba59abbe56e057f20f883e";

        ReturnMsg msg = studentController.crossDomainSetCookie(request, response, "test", value);
        System.out.println(msg.toString());
    }
}

GItHub源码:https://github.com/lovelifeming/Resource/tree/master/SpringBoots/SpringBootDemo

备注:
作者:Shengming Zeng
博客:http://www.cnblogs.com/zengming/
严正声明:
1.由于本博客部分资源来自互联网,版权均归原作者所有。转载的目的是用于学术交流与讨论学习,将不对任何资源负法律责任。
2.若无意中侵犯到您的版权利益,请来信联系我,我会在收到信息后会尽快给予处理!
3.所有资源内容仅供学习交流之用,请勿用作商业用途,谢谢。
4.如有转发请注明出处,来源于http://www.cnblogs.com/zengming/ ,谢谢合作。

分类:

技术点:

相关文章: