JdbcTemplate 的 EasyMock 断言错误 - 意外的方法调用
EasyMock Assertion Error for JdbcTemplate - Unexpected Method call
我正在尝试使用 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
我正在尝试使用 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