英文:
EasyMock Assertion Error for JdbcTemplate - Unexpected Method call
问题
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
.
我正在尝试使用 Easymock 测试一个 jdbctemplate.queryForObject()
调用,但我遇到了一个断言错误:意外的方法调用 JdbcTemplate.queryForObject:期望:1,实际: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.
我尝试参考 Stack Overflow 上其他类似问题的答案,并尝试实施它们,但这并没有帮助,我仍然卡在同一个地方。我还尝试将值传递为 EasyMock.isA()
或 eq()
,但仍然出现相同的错误。请建议我哪里做错了。
I'm using EasyMock 3.5.1
我正在使用 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)
...
请注意,代码中的 "
应该被替换为双引号 "
。如果你需要更多帮助,请告诉我。
英文:
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)
...
答案1
得分: 1
有一个参数错误。
Object[] params = new Object[] { "testucid" }
this.authenticationDaoImpl.checkIfCallExist("testUcid"); // 应该是 testucid
英文:
There is a mistake in the parameter.
Object[] params = new Object[] { "testucid" }
this.authenticationDaoImpl.checkIfCallExist("testUcid"); // should be testucid
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论