Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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 <a href="http://easymock.org/api/easymock/3.0/index.html">API</a> provides the child class with all the methods that it would normally use statically from the EasyMock class, such as createMock(Class class).</p> <p>So as for the updated code</p> <pre><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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload