JdbcTemplate的EasyMock断言错误-意外的方法调用



我正试图使用Easymock测试jdbctemplate.queryForObject()调用,但我得到了一些Assertion error: Unexpected method call JdbcTemplate.queryForObject : expected: 1, actual: 0

我试着在SO上引用其他有类似问题的答案,并试图实现它们,但这并没有帮助,我仍然停留在同一个地方。我还尝试将值传递为EasyMock.isA()eq(),但仍然存在相同的错误。请说明我做错了什么。

我正在使用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);
}
}

错误堆栈跟踪

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)
...

参数中存在错误。

Object[] params = new Object[] { "testucid" }
this.authenticationDaoImpl.checkIfCallExist("testUcid"); // should be testucid

相关内容

最新更新