2 Mockito

Mockito是一个流行的Mocking框架。它使用起来简单,学习成本很低,而且具有非常简洁的API,测试代码的可读性很高。因此它十分受欢迎,用户群越来越多,很多的开源的软件也选择了Mockito。

Jar包下载地址:http://code.google.com/p/mockito/downloads/list

 

Maven

如果项目是通过Maven管理的,需要在项目的Pom.xml中增加如下的依赖:

<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
</dependencies>

2.1 简单示例:

1) 

import static

import
import
import
 
public class
@Test
public void
//创建mock对象,参数可以是类,也可以是接口  
class);  
//设置方法的预期返回值  
"helloworld");  
        String result = list.get(0);  
//验证方法调用(是否调用了get(0))  
        verify(list).get(0);  
//junit测试  
"helloworld", result);  
    }  
}

2)

import static
import
import
import
import
 
public class
@Test
public void
LinkedList mockedList = mock(LinkedList.class) ;
System.out.println(mockedList.get(999)) ;
when(mockedList.get(0)).thenReturn("first") ;
System.out.println(mockedList.get(0)) ;
}
}

以上两个示例运行之后第一个示例没有任何回显,只是将结果与预定值进行对比;对比结果一致则正常运行,对比结果不一致则报错。而第二个示例会在控制台打印信息:

 

java 的MockMultipartFile对应的jar包 mockito jar包_单元测试

2.1.2  mock创建对象

可使用如下语法创建对象,可以对类和接口进行mock对象的创建,创建时可以为mock对象命名。对mock对象命名的好处是调试的时候容易辨认mock对象。创建mock对象不能对final,Anonymous ,primitive类进行mock。

mock(Class<T> classToMock);
mock(Class<T> classToMock, String name)
mock(Class<T> classToMock, Answer defaultAnswer)
mock(Class<T> classToMock, MockSettings mockSettings)
mock(Class<T> classToMock, ReturnValues returnValues)

示例2中创建mock对象:

1. // 模拟LinkedList 的对象 
2. LinkedList mockedList = mock(LinkedList. class );   
3.   
4. // 此时调用get方法,是会返回null,因为还没有对方法调用的返回值做模拟  
5. System.out.println(mockedList.get( 999 ));

 

2.1.3 模拟返回值

因为创建模拟对象时没有对这里调用的get()方法进行返回值模拟,所以该方法的返回值为null,所以示例2中第一次打印结果为null。

对方法返回值进行模拟:

1. // 模拟获取第一个元素时,返回字符串first 
2. when(mockedList.get( 0 )).thenReturn( "first" );   
3.   
4. // 此时打印输出first 
5. System.out.println(mockedList.get( 0 ));

因为已经对方法的返回值进行了模拟,所以第二次打印的值为已定义的模拟值。

2.1.4 异常模拟

也可以对异常方法异常进行模拟:

1. // 模拟获取第二个元素时,抛出RuntimeException
2. when(mockedList.get(1)).thenThrow(new
3.   
4. // 此时将会抛出RuntimeException
5. System.out.println(mockedList.get(1));

在示例2中添加上述代码,对异常进行模拟,运行之后得到如下结果:

 

java 的MockMultipartFile对应的jar包 mockito jar包_单元测试_02

此时,系统会抛出模拟的异常,当然没有返回值的方法也是可以模拟其抛出异常的:

1. doThrow( new

2. mockList.clear();

此时,系统抛出异常:

 

java 的MockMultipartFile对应的jar包 mockito jar包_单元测试_03

java 的MockMultipartFile对应的jar包 mockito jar包_List_04

 

2.1.5 方法匹配参数模拟

也可以对方法的参数匹配模拟:

1. // anyInt()匹配任何int参数,这意味着参数为任意值,其返回值均是element 
2. when(mockedList.get(anyInt())).thenReturn( "element" );   
3.   
4. // 此时打印是element 
5. System.out.println(mockedList.get( 0 ));

结果:

 

java 的MockMultipartFile对应的jar包 mockito jar包_单元测试_05

 

 

2.1.6 附表(更多灵活的参数搭配)


 

方法一览

static boolean
anyBoolean() 
 any boolean, Boolean or null.static byte
anyByte() 
 any byte, Byte or nullstatic char
anyChar() 
 any char, Character or null.static java.util.Collection
anyCollection() 
 any Collection or null.static double
anyDouble() 
 any double, Double or null.static float
anyFloat() 
 any float, Float or null.static int
anyInt() 
 any int, Integer or null.static java.util.List
anyList() 
 any List or null.static long
anyLong() 
 any long, Long or null.static java.util.Map
anyMap() 
  any Map or null.static<T> T
anyObject() 
 any Object or null.static short
anyShort() 
 any short, Short or null.static java.lang.String
anyString() 
any String or null.
Static<T> T
argThat(org.hamcrest.Matcher<T> matcher)
Allows creating custom argument matchers.
static boolean
booleanThat(org.hamcrest.Matcher<java.lang.Boolean> matcher)
Allows creating custom argument matchers.
static byte
byteThat(org.hamcrest.Matcher<java.lang.Byte> matcher)
Allows creating custom argument matchers.
static char
charThat(org.hamcrest.Matcher<java.lang.Character> matcher) 
Allows creating custom argument matchers.
static java.lang.String
contains(java.lang.String substring) 
String argument that contains the given substring.
static double
doubleThat(org.hamcrest.Matcher<java.lang.Double> matcher) 
Allows creating custom argument matchers.
static java.lang.String
endsWith(java.lang.String suffix) 
String argument that ends with the given suffix.
static boolean
eq(boolean value) 
boolean argument that is equal to the given value.
static byte
eq(byte value) 
byte argument that is equal to the given value.
static char
eq(char value) 
char argument that is equal to the given value.
static double
eq(double value) 
double argument that is equal to the given value.
static float
eq(float value) 
float argument that is equal to the given value.
static int
eq(int value) 
int argument that is equal to the given value.
static long
eq(long value) 
long argument that is equal to the given value.
static short
eq(short value) 
short argument that is equal to the given value.
static<T> T
eq(T value) 
Object argument that is equal to the given value.
static float
floatThat(org.hamcrest.Matcher<java.lang.Float> matcher) 
Allows creating custom argument matchers.
static int
intThat(org.hamcrest.Matcher<java.lang.Integer> matcher) 
Allows creating custom argument matchers.
static<T> T
isA(java.lang.Class<T> clazz) 
Object argument that implements the given class.
static java.lang.Object
isNotNull() 
not null argument.
static java.lang.Object
isNull() 
null argument.
static long
longThat(org.hamcrest.Matcher<java.lang.Long> matcher) 
Allows creating custom argument matchers.
static java.lang.String
matches(java.lang.String regex) 
String argument that matches the given regular expression.
static java.lang.Object
notNull() 
not null argument.
static<T> T
 
refEq(T value) 
Object argument that is reflection-equal to the given value.
static<T> T
same(T value) 
Object argument that is the same as the given value.
static short
shortThat(org.hamcrest.Matcher<java.lang.Short> matcher) 
Allows creating custom argument matchers.
static java.lang.String
startsWith(java.lang.String prefix) 
String argument that starts with the given prefix.


 


从java.lang.Object类继承的方法

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait






2.1.7 方法调用次数验证

1. // 调用add一次 
2. mockedList.add( "once" );   
3.   
4. // 下面两个写法验证效果一样,均验证add方法是否被调用了一次 
5. verify(mockedList).add( "once" );   
6. verify(mockedList, times( 1 )).add( "once" );

验证结果为:

 

java 的MockMultipartFile对应的jar包 mockito jar包_List_06