Mock测试接口

Mock使用场景git

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

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

  (3)编写知足全部外部依赖的测试可能很复杂,复杂到不值得编写,Mock模拟内部或外部依赖能够帮助咱们解决这些问题数据库

  (4)隔离当前方法使用的的全部依赖,让咱们更加专一于单个单元,忽略其调用的代码的内部工做原理cookie

Mock的好处:网络

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

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

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

  经常使用的Mock工具备jMock 、EasyMock 、Mockito等,但都有一个共同的缺点:不能mock静态、final、私有方法等。而PowerMock可以完美的弥补其余Mock工具的不足,若是须要mock静态方法。须要引入PowerMock。spring-boot

  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/ ,谢谢合做。

相关文章
相关标签/搜索