Wow didnt knew I could partially mock a method on easy mock. Earlier i used to inherit the class and override methods to mock and have AtomicIntegers to determine which methods were called or not. Recently I realized I was dumb and I should use easy mock to do that.
In below example I needed to very that if I get a json parsing exception for data stored in memcached then I need to delete the key from memcached. To test this I had to preserve all the code in MemCacheWrapper class but just mock the deleted method. here is how I did it.
@Test
public void testDeleteOldKeyOnJsonParse() throws Exception {
String key="user.1";
MemCacheWrapper sut = EasyMock.createMockBuilder(MemCacheWrapper.class).addMockedMethod("delete").createMock();
EasyMock.expect(sut.delete(key)).andReturn(true);
EasyMock.replay(sut);
sut.convertJsonToObject(key, User.class, "crap");
EasyMock.verify(sut);
}
In below example I needed to very that if I get a json parsing exception for data stored in memcached then I need to delete the key from memcached. To test this I had to preserve all the code in MemCacheWrapper class but just mock the deleted method. here is how I did it.
@Test
public void testDeleteOldKeyOnJsonParse() throws Exception {
String key="user.1";
MemCacheWrapper sut = EasyMock.createMockBuilder(MemCacheWrapper.class).addMockedMethod("delete").createMock();
EasyMock.expect(sut.delete(key)).andReturn(true);
EasyMock.replay(sut);
sut.convertJsonToObject(key, User.class, "crap");
EasyMock.verify(sut);
}
Comments
Post a Comment