I have been trying to test my DAOs in a Spring 2.5 environment using EasyMock 2.3. I get a very strange behaviour when testing, it says that it expected a call that it didn't get, although it has got called.
// Create the DAO
PrizeDaoImpl prizeDao = new PrizeDaoImpl();
// Create mock objects
DataSource dataSourceMock = createMock(DataSource.class);
Connection connectionMock = createMock(Connection.class);
PreparedStatement preparedStatementMock = createMock(PreparedStatement.class);
// Define expectations
expect(dataSourceMock.getConnection()).andReturn(connectionMock);
expect(dataSourceMock.getConnection("Test", "Test")).andReturn(connectionMock);
expect(connectionMock.prepareStatement(PrizeDaoImpl.FIND_PRIZES_FOR_PLAYER)).andReturn(preparedStatementMock);
preparedStatementMock.setObject(1, 90);
preparedStatementMock.setObject(2, 12345); // This is the call that it does not receive, it says
// End recording
replay(preparedStatementMock, connectionMock, dataSourceMock);
// Set mock object in my DAO (injection)
prizeDao.setDataSource(dataSourceMock);
// Do the call
prizeDao.getPrizesForPlayer(12345, 90);
verify(dataSourceMock, connectionMock, preparedStatementMock);
The result is an error:
java.lang.AssertionError:
Unexpected method call setObject(2, 12345):
setObject(2, 12345): expected: 1, actual: 0
If i remove the "preparedStatementMock.setObject(2, 12345);" statement, an eror is thrown about the unexpected call.
In the DAO, it calles JDBCTemplate, setting the two arguments (in reversed order)
public List<Prize> getPrizesForPlayer(long playerId, int noOfDaysBackInTime) {
List<Map<String, Object>> dbResult = getSimpleJdbcOperations().queryForList(FIND_PRIZES_FOR_PLAYER, noOfDaysBackInTime, playerId);
...
Has anyone else experienced the same error, when setting two expectations after eachother?