Note that there are some explanatory texts on larger screens.

plurals
  1. POCan a JMock mock object return another mock object?
    text
    copied!<p>I'm writing tests for an application using the iBatis DAO framework. The app is running in a java 1.4 environment, so I'm using legacy versions of everything (JDK 1.4, JUnit3, iBatis 2.3 and JMock 1.2). </p> <p>In my <code>MockObjectTestCase</code> subclass I have this test</p> <pre><code>public void testInsert() throws Exception { Mock mockDao = mock(TblPpvFiltersDao.class); mockDao.expects(once()).method("insert"); Mock mockDaoManager = mock(DaoManager.class); mockDaoManager.expects(once()).method("getDao") .with(eq(TblPpvFiltersDao.class)) .will(returnValue((TblPpvFiltersDao) mockDao.proxy())); PpvFiltersService service = new PpvFiltersServiceImpl( (DaoManager) mockDaoManager.proxy()); service.insert(new PpvFiltersVO()); } </code></pre> <p>which should verify that the service object will ask the <code>DaoManager</code> for a DAO object and call the <code>insert</code> method on it. The test fails with the error message</p> <pre><code>...DynamicMockError: mockDaoManager: tried to return an incompatible value: expected a com.ibatis.dao.client.Dao but returned a $Proxy0 </code></pre> <p>Trying to cast the <code>mockDao</code> object either to either <code>com.ibatis.dao.client.Dao</code> or <code>com.ibatis.dao.client.template.SqlMapDaoTemplate</code> ends in a <code>ClassCastException</code>.</p> <p>What am I missing?</p> <p><strong>Update:</strong> nothing changes converting the code to use JDK 1.6, JUnit 4 and JMock2. This code</p> <pre><code>@Test public void testInsert() throws Exception { final PpvFiltersVO theFilter = new PpvFiltersVO(new Integer(42)); final TblPpvFiltersDao mockDao = context.mock(TblPpvFiltersDao.class); final DaoManager mockDaoManager = context.mock(DaoManager.class); context.checking(new Expectations() {{ oneOf (mockDaoManager).getDao(TblPpvFiltersDao.class); will(returnValue(mockDao)); oneOf (mockDao).insert(theFilter); }}); PpvFiltersService service = new PpvFiltersServiceImpl(mockDaoManager); service.insert(theFilter); } </code></pre> <p>results in this error message:</p> <pre><code>java.lang.IllegalStateException: tried to return a $Proxy6 from a method that can only return a com.ibatis.dao.client.Dao </code></pre> <p>maybe I'm missing something obvious here, but the code above comes almost straight from the JMock examples at <a href="http://www.jmock.org/getting-started.html" rel="nofollow noreferrer">http://www.jmock.org/getting-started.html</a>.</p> <p>Any ideas?</p> <p><strong>Fixed</strong> Of course it was something obvious. <code>TblPpvFiltersDao</code> above needs to extend the <code>com.ibatis.dao.client.Dao</code> marker interface. D'oh.</p>
 

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