mokito详解三

Mockito 1.8.5+新特性解析:@Captor、@Spy与验证优化
本文详细介绍了Mockito从1.8.5版本开始引入的新特性,包括@Captor用于捕获泛型参数避免警告,@Spy用于创建部分模拟对象,@InjectMocks配合@Spy实现自动注入,超时验证,成员变量的自动注入,一行式存根,以及对mock和spy对象的便捷验证方法。此外,还提及了Mockito从1.10.12版本起支持抽象类的mock和spy,并提供了自定义错误描述以改进失败时的反馈信息。

睡醒了,接着来

  • 新的注解(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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值