今天写单元测试想换种口味,jmock写多了未免有点乏味了,所以今天自学了jmockit,感觉超级强大,下面我就来好好讲解下jmockit吧。
@Tested
GoldEggActiveManagerImpl impl = new GoldEggActiveManagerImpl();
@Injectable
GoldeggAwardDao goldeggAwardDao;
@Injectable
UserGoldeggAwardDao usergoldeggAwardDao;
@Injectable
UserGoldeggShareDao usergoldeggShareDao;
@Tested表示要测试的类
@Injectable表示将Dao自动注入到@Tested注解的类中(就是不需要new了)
jmockit有两种生成期望的方式,一种是Expectations,另一种是NonStrictExpectations,前者是严格性的校验,至少会执行一次,后者是非严格性校验,可以执行N次也可以不执行
啥也不说了,上代码,下面先看Expectations:
@Test
public void testGetUserCodeInfo() {
String inviteCode = "aaaaaa";
new Expectations(GoldEggActiveManagerImpl.class) {
// 构造函数可以传入Class或Instance实例
{
UserCode userCode = new UserCode();
userCodeDao.selectWithParams((UserCode)any);
returns(userCode);
result = new BizException();
times = 2;
}
};
UserCode userCode1 = impl.getUserCodeInfo(inviteCode, ActiveEnumUtils.POUND_GOLD_EGG);
Assert.assertNotNull(userCode1);
UserCode userCode12 = null;
try{
userCode12 = impl.getUserCodeInfo(inviteCode, ActiveEnumUtils.POUND_GOLD_EGG);
}catch(Exception e){
Assert.assertNull(