I'm trying to test a jdbctemplate.queryForObject()
call using Easymock but I'm getting some Assertion error: Unexpected method call JdbcTemplate.queryForObject : expected: 1, actual: 0
I tried to refer other answers on SO that had some similar issue and tried to implement them, but that didn't help and I'm still stuck at same place. I also tried passing the values as EasyMock.isA()
or eq()
but still same error. Please suggest what I'm doing wrong.
I'm using EasyMock 3.5.1
AuthenticationDaoImpl.java
public boolean checkIfCallExist(String ucid){
String decision = null;
String sql = "select count(*) from tablename where ucid=?"
decision = jdbcTemplate.queryForObject(sql, new Object[]{ucid}, String.class);
return (Integer.parseInt(decision)>0);
}
AuthenticationDaoImplTest.java
@RunWith(EasyMockRunner.class)
public class AuthenticationDaoImplTest {
@TestSubject
private AuthenticationDaoImpl authenticationDaoImpl = new AuthenticationDaoImpl();
@Mock
JdbcTemplate jdbcTemplateObject;
@Test
public void checkIfCallExistTest(){
String sql = "select count(*) from tablename where ucid=?";
Object[] params = new Object[] { "testucid" };
EasyMock.expect(this.jdbcTemplateObject.queryForObject(sql, params, String.class)).andReturn("0");
EasyMock.replay(this.jdbcTemplateObject);
boolean res = this.authenticationDaoImpl.checkIfCallExist("testUcid");
assertEquals(false, res);
}
}
Error Stacktrace
java.lang.AssertionError:
Unexpected method call JdbcTemplate.queryForObject("select count(*) from tablename where ucid=?", ["testUcid"], class java.lang.String):
JdbcTemplate.queryForObject("select count(*) from tablename where ucid=?", ["testucid"], class java.lang.String): expected: 1, actual: 0
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94)
at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:95)
at org.springframework.jdbc.core.JdbcTemplate$$EnhancerByCGLIB$$a2fb2844.queryForObject(<generated>)
at org.authenticationint.dao.AuthenticationDaoImpl.checkIfCallExist(AuthenticationDaoImpl.java:53)
at org.authenticationint.dao.AuthenticationDaoImplTest.checkIfCallExistTest(AuthenticationDaoImplTest.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
There is a mistake in the parameter.
Object[] params = new Object[] { "testucid" }
this.authenticationDaoImpl.checkIfCallExist("testUcid"); // should be testucid