睡醒了,接着来
- 新的注解(since 1.8.5)@Captor, @Spy, @InjectMocks
@Captor当捕获是一个严重的泛型类的参数和你想避免编译器警告
public class Test{
@Captor ArgumentCaptor<AsyncCallback<Foo>> captor;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
}
@Test public void shouldDoSomethingUseful() {
//...
verify(mock).doStuff(captor.capture());
assertEquals("foo", captor.getValue());
}
}
@Spy - 你可以用来替换掉 spy(Object).
@InjectMocks - 自动注入mock对象,或者注入spy成员变量中
@InjectMocksye也可以配合@Spy一起使用,意味着mockito会将mock注入到部分模拟测试中
public class ArticleManagerTest extends SampleBaseTestCase {
@Mock private ArticleCalculator calculator;
@Mock(name = "database") private ArticleDatabase dbMock; // note the mock name attribute
@Spy private UserProvider userProvider = new ConsumerUserProvider();
@InjectMocks private ArticleManager manager;
@Test public void shouldDoSomething() {
manager.initiateArticle();
verify(database).addListener(any(ArticleListener.class));
}
}
public class SampleBaseTestCase {
@Before public void initMocks() {
MockitoAnnotations.initMocks(this);
}
}
- 超时验证(since 1.8.5)
//passes when someMethod() is called within given time span
verify(mock, timeout(100)).someMethod();
//above is an alias to:
verify(mock, timeout(100).times(1)).someMethod();
//passes when someMethod() is called *exactly* 2 times within given time span
verify(mock, timeout(100).times(2)).someMethod();
//passes when someMethod() is called *at least* 2 times within given time span
verify(mock, timeout(100).atLeast(2)).someMethod();
//verifies someMethod() within given time span using given verification mode
//useful only if you have your own custom verification modes.
verify(mock, new Timeout(100, yourOwnVerificationMode)).someMethod();
- 成员变量的自动注入(since 1.9.0)
mokito会尝试着初始化@Spy里面的@InjectMocks 变量,通过构造方法,set方法或者变量注入
//instead:
@Spy BeerDrinker drinker = new BeerDrinker();
//you can write:
@Spy BeerDrinker drinker;
//same applies to @InjectMocks annotation:
@InjectMocks LocalPub;
- one-liner stubs
public class CarTest {
Car boringStubbedCar = when(mock(Car.class).shiftGear()).thenThrow(EngineNotStarted.class).getMock();
@Test public void should... {}
mokito允许你在创建mock对象的时候subbing,允许你在一行code里面创建stub,这样能够是代码看上去更加简洁
- mock对象的校验(since 1.9.5)
Mockito.mockingDetails(someObject).isMock();
Mockito.mockingDetails(someObject).isSpy();
方法返回boolean值,校验一个对象是否是mock or spy对象
- 抽象类的mock or spy(since 1.10.12)
//convenience API, new overloaded spy() method:
SomeAbstract spy = spy(SomeAbstract.class);
//Robust API, via settings builder:
OtherAbstract spy = mock(OtherAbstract.class, withSettings()
.useConstructor().defaultAnswer(CALLS_REAL_METHODS));
//Mocking a non-static inner abstract class:
InnerAbstract spy = mock(InnerAbstract.class, withSettings()
.useConstructor().outerInstance(outerInstance).defaultAnswer(CALLS_REAL_METHODS));
校验错误的描述信息 (since 2.0.0)
// will print a custom message on verification failure
verify(mock, description(“This will print on failure”)).someMethod();// will work with any verification mode
verify(mock, times(2).description(“someMethod should be called twice”)).someMethod();