mockito学习记录

mockito学习记录

 

1 配置 Mock 对象

List mock = mock( List.class );  
when( mock.get(0) ).thenReturn( 1 );  
assertEquals( "预期返回1", 1, mock.get( 0 ) );// mock.get(0) 返回 1  

 我们看到 List 为 Java.util.List 是接口,并不是实现类,但这不妨碍我们使用它作为我们的“打桩”对象,——当然你也可以使用实现类,传入 mock(obj) 方法中。这里提到的是"打桩(Stub,也有人称其为“存根”)"的概念,是一个形象的说法,就是把所需的测试数据塞进对象中,适用于基于状态的(state-based)测试,关注的是输入和输出。Mockito 中 when(…).thenReturn(…)  这样的语法来定义对象方法和参数(输入),然后在 thenReturn 中指定结果(输出)。此过程称为 Stub 打桩。一旦这个方法被 stub 了,就会一直返回这个 stub 的值。

注意以下几点:

对于 static 和 final 方法, Mockito 无法对其 when(…).thenReturn(…) 操作。

当我们连续两次为同一个方法使用 stub 的时候,他只会只用最新的一次。

2 打桩支持迭代风格的返回值设定,例如

// 第一种方式   
when(i.next()).thenReturn("Hello").thenReturn("World");  
// 第二种方式  
when(i.next()).thenReturn("Hello", "World");  
// 第三种方式,都是等价的  
when(i.next()).thenReturn("Hello");  
when(i.next()).thenReturn("World"); 

 第一次调用 i.next() 将返回 ”Hello”,第二次的调用会返回 ”World”。

3 返回值为void的方法

Nothing().when(obj).notify();  
// 或直接  
when(obj).notify();

4 强行抛出异常

when(i.next()).thenThrow(new RuntimeException());  
doThrow(new RuntimeException()).when(i).remove(); // void 方法的  
// 迭代风格   
doNothing().doThrow(new RuntimeException()).when(i).remove(); // 第一次调用 remove 方法什么都不做,第二次调用抛出 RuntimeException 异常。

  

5 模拟传入的参数 argument matchers

提供 argument matchers 机制,例如 anyString() 匹配任何 String 参数,anyInt() 匹配任何 int 参数,anySet() 匹配任何 Set,any() 则意味着参数为任意值。例子如下,

when(mockedList.get(anyInt())).thenReturn("element");     
System.out.println(mockedList.get(999));// 此时打印是 element    

 再进一步,自定义类型也可以,如 any(User.class)

6 Answer接口-获取随机返回的结果

final Map<String, Object> hash = new HashMap<String, Object>();  
Answer<String> aswser = new Answer<String>() {    
    public String answer(InvocationOnMock invocation) {    
        Object[] args = invocation.getArguments();    
        return hash.get(args[0].toString()).toString();    
    }   
};  
  
when(request.getAttribute("isRawOutput")).thenReturn(true);   
when(request.getAttribute("errMsg")).thenAnswer(aswser);   
when(request.getAttribute("msg")).thenAnswer(aswser);  

7 验证 Verify

前面提到的 when(……).thenReturn(……) 属于状态测试,某些时候,测试不关心返回结果,而是侧重方法有否被正确的参数调用过,这时候就应该使用 验证方法了。从概念上讲,就是和状态测试所不同的“行为测试”了。

一旦使用 mock() 对模拟对象打桩,意味着 Mockito 会记录着这个模拟对象调用了什么方法,还有调用了多少次。最后由用户决定是否需要进行验证,即 verify() 方法。

Map mock = Mockito.mock( Map.class );  
when( mock.get( "city" ) ).thenReturn( "广州" );  
// 关注参数有否传入  
verify(mock).get( Matchers.eq( "city" ) );  
// 关注调用的次数  
verify(mock, times( 2 ));  

 Mockito 除了提供 times(N) 方法供我们调用外,还提供了很多可选的方法:

never() 没有被调用,相当于 times(0)

atLeast(N) 至少被调用 N 次

atLeastOnce() 相当于 atLeast(1)

atMost(N) 最多被调用 N 次

猜你喜欢

转载自bbwang8088.iteye.com/blog/2356473
今日推荐