开发者

EasyMock returns Null for Expected Method

I have am having a problem with EasyMock returning null for an expected (defined) method call.

Creation of the mocked Object

mock = EasyMock.createMock(DAO.class);

Mock Set up in unit test.

expect(mock.update(myObj).andReturn(myObjUpdated).once();
replayAll();
service.setDao(mock);
service.processData(myObj);
verifyAll();

processData method simply calls

MyObject objUpdated = dao.update(myObj);

here is the interface that the mock is being built from.

public interface DAO {
   public <ENTITY> ENTITY update(ENTITY entity);
}

I am pretty confused by what might be causing the problem. I have confirmed that 'obj' is the same object as I defined in the unit test. I have also not experienced this problem (that I am aware of) with any other methods that mocked.

Could the problem pos开发者_StackOverflow中文版sibly be with the Object that is being passed in?

Thanks in advance. I am really not sure what other information might be helpful to you here.

edit: this is the test class (and as it turns out where my misunderstanding began)

public class TestMyService extends EasyMockHelper {...}


So it turns out that my main problem isn't with the expectations or even with the creation of the mock object. I had a fundamental misunderstanding about how the EasyMockSupport class which my test is extending functions. This isn't covered very well in the documentation, but if you exam the examples a bit more closely my error became obvious.

The EasyMockSupport class gives my test class access to methods such as replayAll(), verifyAll(), and resetAll(). what these do is allow me to now worry about manually controlling each created mock object. However, what the documentation failed to mention was that you have to create you Mock object USING the methods provided by the EasyMockSupport class so that it can properly register the controls. ((this makes total sense btw, I simply wasn't reading it anywhere)). The EasyMockSupport class if you look into the API provides the child class with all the methods that it would normally use statically from the EasyMock class, such as createMock(Class class).

So as for the updated code

public class TestMyService extends EasyMockSupport {
   private MyService service;
   private MyDao dao;

   private MyObject myObj;

   @Before public void setUp() {
      service = new MyService();

      // THIS IS THE KEY
      mock = createMock(IDao.class); //CORRECT
      // mock = EasyMock.createMock(IDao.class); //WRONG

      service.setDao(mock);
      myObj = new MyObject("expectedData");
   }
   @After public void tearDown() {
      verifyAll();
   }
   @Test public void testMyService() {
      expect(mock.update(myObj)).andReturn(myObj);
      replayAll();
      service.myService(myObj);
   }
}

public class MyService() {
   private IDao dao;
   public void setDao(IDao dao) {this.dao = dao; }
   public MyObject myService(MyObject myObj) {
      return dao.update(myObj);
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜