The Mock Anything Toolkit for Java--JMockit
特征1.满足常规mock框架的功能
2.mock static 以及final 的类和方法。
3.支持依赖注入
下面看代码例子:
@Tested
private DistributorService distributorService;
@Mocked
private DistributorDao distributordao;
@Mocked
private DistributorAndAreaRelationDao relationDao;
@Test
public void edit(@Mocked final IConnection conn,@Mocked final CacheService cacheService) {
String json = "{\"id\":1,\"num\":\"120020\",\"name\":\"马云2\",\"remark\":\"随便写点\",\"area\":[{\"pv\":\"110000\",\"t\":1},{\"pv\":\"110000\",\"city\":\"11011\",\"t\":2},{\"pv\":\"120000\",\"city\":\"120100\",\"t\":1}],\"header\":{\"uid\":\"aaaaaa\",\"cid\":1,\"clid\":\"Zhabei\",\"uip\":\"aaaaaaaaaa\",\"rid\":\"6ba678cb-b2a5-426a-ad86-bf491aed172e\"}}";
DistributorRequest distributorRequest = JSON.parseObject(json.toString(), DistributorRequest.class);
try {
new Expectations() {
{
distributordao.checkUniqueDistributorNumber(conn, (Distributor) any, true);
result = false;
distributordao.editDistributor(conn, (Distributor)any);
result=true;
relationDao.save(conn,(DistributorAndAreaRelation)any);
result=100;
relationDao.delete(conn, anyString, anyInt);
result=true;
}
};
new MockUp<SystemInitialization>() {
@Mock
public CacheService getCache() {
return cacheService;
}
};
new MockUp<ConnectionManager>() {
@Mock
public IConnection getConnectionOpenTransaction() {
return conn;
}
};
distributorService.edit(distributorRequest);
Assert.assertTrue(true);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("DistributorServiceTest edit mock handle failed!");
}
}