1
我在Mockito
中有几个测试,我尝试将它转换为EasyMock
,但我不知道如何。在Mockito
我可以使用spy
,如何实现这样的东西。如何在EasyMock中替代间谍
public class TTTCollection {
private MongoCollection mongoCollection;
protected MongoCollection getMongoCollection() {
return mongoCollection;
}
private static final String dbName = "TTT";
private static final String collectionName = "ruchy";
public TTTCollection(){
DB db = new MongoClient().getDB(dbName);
mongoCollection = new Jongo(db).getCollection(collectionName);
}
public boolean deletedb() {
try {
getMongoCollection().drop();
return true;
} catch (Exception e) {
return false;
}
}
public boolean save(TTTObject object) {
try {
getMongoCollection().save(object);
return true;
} catch (Exception e) {
return false;
}
和试验中的Mockito:
public class TTTCollectionTest {
TTTCollection collection;
TTTObject object;
MongoCollection mongoCollection;
@Before
public void Setup(){
collection = spy(new TTTCollection());
mongoCollection = mock(MongoCollection.class);
object = new TTTObject(1,2, 2, "x");
}
@Test
public void testDeleteCollection(){
doReturn(mongoCollection).when(collection).getMongoCollection();
assertTrue(collection.deletedb());
}
@Test
public void testSave() {
doReturn(mongoCollection).when(collection).getMongoCollection();
assertTrue(collection.save(object));
}
和
public class TTTCollectionEMTest extends EasyMockSupport {
TTTCollection collection;
TTTObject object;
MongoCollection mongoCollection;
@Before
public void Setup(){
mongoCollection = EasyMock.createMock(MongoCollection.class);
collection = new TTTCollection(); // how to spy it ?
object = new TTTObject(1,2, 2, "x");
}
@Test
public void testDeleteCollection(){
EasyMock.expect(collection.getMongoCollection()).andReturn(mongoCollection);
replayAll();
assertTrue(collection.deletedb());
}
确实[这个问题](http://stackoverflow.com/questions/28448347/)和它的回答可以帮助你? – Laf
不多,我无法根据需要调整代码 – user7902809