zoukankan      html  css  js  c++  java
  • Mockito框架入门教程(一)

    官网: http://mockito.org

    API文档:http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html

    项目源码:https://github.com/mockito/mockito

     

    在做单元测试的时候,有的时候用到的一些类,我们构造起来不是那么容易,比如HttpRequest,或者说某个Service依赖到了某个Dao,想构造service还得先构造dao,这些外部对象构造起来比较麻烦。 所以出现了Mock! 我们可以用 Mock 工具来模拟这些外部对象,来完成我们的单元测试。

    关于什么时候需要Mock对象,Tim Mackinnon给我们了一些建议:

      ----- 真实对象具有不可确定的行为(产生不可预测的结果,如股票的行情)

      ----- 真实对象很难被创建(比如具体的web容器)

      ----- 真实对象的某些行为很难触发(比如网络错误)

      ----- 真实情况令程序的运行速度很慢

      ----- 真实对象有用户界面

      ----- 测试需要询问真实对象它是如何被调用的(比如测试可能需要验证某个回调函数是否被调用了)

      ----- 真实对象实际上并不存在(当需要和其他开发小组,或者新的硬件系统打交道的时候,这是一个普遍的问题)

     

      实现Mock技术的优秀开源框架有很多,下面以Mockito为例,用几个简单例子来介绍Mock工具的基本使用:

    首先添加maven依赖

    加载mockito依赖:

          <dependency>

              <groupId>org.mockito</groupId>

              <artifactId>mockito-all</artifactId>

              <version>1.9.5</version>

              <scope>test</scope>

          </dependency>

    当然mockito需要junit或testng配合使用

          <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.11</version>

            <scope>test</scope>

          </dependency>

    testng依赖此处省略;

    然后为了使代码更简洁,最好在测试类中导入静态资源

    import static org.mockito.Mockito.*;

    import static org.junit.Assert.*;

    使用mockito来做测试:

    下面我们开始使用mockito来做测试

    1、验证行为

           @Test

           public void verify_behaviour(){

                  //模拟创建一个List对象

                  List mock = mock(List.class);

                  //使用mock的对象

                  mock.add(1);

                  mock.clear();

                  //验证add(1)和clear()行为是否发生

                  verify(mock).add(1);

                  verify(mock).clear();

           }

    2、模拟我们所期望的结果

           @Test

           public void when_thenReturn(){

                  //mock一个Iterator类

                  Iterator iterator = mock(Iterator.class);

                  //预设当iterator调用next()时第一次返回hello,第n次都返回world

                  when(iterator.next()).thenReturn("hello").thenReturn("world");

                  //使用mock的对象

                  String result = iterator.next() + " " + iterator.next() + " " + iterator.next();

                  //验证结果

                  assertEquals("hello world world",result);

           }

           @Test(expected = IOException.class)

           public void when_thenThrow() throws IOException {

                  OutputStream outputStream = mock(OutputStream.class);

                  OutputStreamWriter writer = new OutputStreamWriter(outputStream);

                  //预设当流关闭时抛出异常

                  doThrow(new IOException()).when(outputStream).close();

                  outputStream.close();

           }

    3、参数匹配

           @Test

           public void with_arguments(){

                  Comparable comparable = mock(Comparable.class);

                  //预设根据不同的参数返回不同的结果

                  when(comparable.compareTo("Test")).thenReturn(1);

                  when(comparable.compareTo("Omg")).thenReturn(2);

                  assertEquals(1, comparable.compareTo("Test"));

                  assertEquals(2, comparable.compareTo("Omg"));

                  //对于没有预设的情况会返回默认值

                  assertEquals(0, comparable.compareTo("Not stub"));

           }

    除了匹配制定参数外,还可以匹配自己想要的任意参数

           @Test

           public void with_unspecified_arguments(){

                  List list = mock(List.class);

                  //匹配任意参数

                  when(list.get(anyInt())).thenReturn(1);

                  when(list.contains(argThat(new IsValid()))).thenReturn(true);

                  assertEquals(1, list.get(1));

                  assertEquals(1, list.get(999));

                  assertTrue(list.contains(1));

                  assertTrue(!list.contains(3));

           }

           private class IsValid extends ArgumentMatcher<List>{

                  @Override

                  public boolean matches(Object o) {

                         return o == 1 || o == 2;

                  }

           }

    需要注意的是如果你使用了参数匹配,那么所有的参数都必须通过matchers来匹配

           @Test

           public void all_arguments_provided_by_matchers(){

                  Comparator comparator = mock(Comparator.class);

                  comparator.compare("nihao","hello");

                  //如果你使用了参数匹配,那么所有的参数都必须通过matchers来匹配

                  verify(comparator).compare(anyString(),eq("hello"));

                  //下面的为无效的参数匹配使用

                  //verify(comparator).compare(anyString(),"hello");

           }

    4、验证确切的调用次数

           @Test

           public void verifying_number_of_invocations(){

                  List list = mock(List.class);

                  list.add(1);

                  list.add(2);

                  list.add(2);

                  list.add(3);

                  list.add(3);

                  list.add(3);

                  //验证是否被调用一次,等效于下面的times(1)

                  verify(list).add(1);

                  verify(list,times(1)).add(1);

                  //验证是否被调用2次

                  verify(list,times(2)).add(2);

                  //验证是否被调用3次

                  verify(list,times(3)).add(3);

                  //验证是否从未被调用过

                  verify(list,never()).add(4);

                  //验证至少调用一次

                  verify(list,atLeastOnce()).add(1);

                  //验证至少调用2次

                  verify(list,atLeast(2)).add(2);

                  //验证至多调用3次

                  verify(list,atMost(3)).add(3);

           </span>}

    5、模拟方法体抛出异常

           @Test(expected = RuntimeException.class)

           public void doThrow_when(){

                  List list = mock(List.class);

                  doThrow(new RuntimeException()).when(list).add(1);

                  list.add(1);

           }

    6、验证执行顺序

           @Test

           public void verification_in_order(){

                  List list = mock(List.class);

                  List list2 = mock(List.class);

                  list.add(1);

                  list2.add("hello");

                  list.add(2);

                  list2.add("world");

                  //将需要排序的mock对象放入InOrder

                  InOrder inOrder = inOrder(list,list2);

                  //下面的代码不能颠倒顺序,验证执行顺序

                  inOrder.verify(list).add(1);

                  inOrder.verify(list2).add("hello");

                  inOrder.verify(list).add(2);

                  inOrder.verify(list2).add("world");

           }

    7、确保模拟对象上无互动发生

           @Test

           public void verify_interaction(){

                  List list = mock(List.class);

                  List list2 = mock(List.class);

                  List list3 = mock(List.class);

                  list.add(1);

                  verify(list).add(1);

                  verify(list,never()).add(2);

                  //验证零互动行为

                  verifyZeroInteractions(list2,list3);

           }

    后续还有....

    生命很短,请让生活更精彩一些!
  • 相关阅读:
    二元查找树转化成排序的双向链表——要求不创建新的节点
    MySQL 通配符学习小结
    HDU 1596 find the safest road (最短路)
    webapp开发调试环境--weinre配置
    全局钩子具体解释
    英尺到米的换算
    apache2.2 虚拟主机配置
    HTTP Digest authentication
    前端project师的修真秘籍(css、javascript和其他)
    CODE:BLOCK中的CreateProcess: No such file or directory
  • 原文地址:https://www.cnblogs.com/Aaron-007/p/10441492.html
Copyright © 2011-2022 走看看