EasyMock returns Null for expected method

I am having trouble returning EasyMock null for the expected (specific) method call.

Create a mocking object

mock = EasyMock.createMock(DAO.class); 

Layout Install in unit test.

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

The processData method simply calls

 MyObject objUpdated = dao.update(myObj); 

here is the interface from which the layout is built.

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

I'm pretty confused about what might cause the problem. I confirmed that "obj" is the same object that I defined in unit test. I also did not experience this problem (what I know) with any other methods that were taunted.

Could the problem be with the object that is being passed in?

Thanks in advance. I'm really not sure what other information might be useful to you here.

edit: this is a test class (and, as it turns out, where did my misunderstanding begin)

 public class TestMyService extends EasyMockHelper {...} 
+4
source share
1 answer

So it turns out that my main problem is not about expectations or even when creating a mock object. I had a fundamental misunderstanding about how the EasyMockSupport class, which extends my tests. This is not well described in the documentation, but if you study the examples more closely, my mistake will become apparent.

The EasyMockSupport class gives my test class access to methods like replayAll (), verifyAll (), and resetAll (). that it allows me now to worry about manually managing each created mock object. However, what was not mentioned in the documentation was that you need to create a Mock object. USE the methods provided by the EasyMockSupport class so that it can correctly register the controls. ((this makes a general sense, I just didn’t read anything)). The EasyMockSupport class, if you look at the API , provides a child class for all the methods that it will usually use statically from the EasyMock class, for example 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); } } 
+7
source

Source: https://habr.com/ru/post/1335467/


All Articles