【问题标题】:EasyMock Assertion Error for JdbcTemplate - Unexpected Method callJdbcTemplate 的 EasyMock 断言错误 - 意外的方法调用
【发布时间】:2020-08-07 11:08:30
【问题描述】:

我正在尝试使用 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)
    ...

【问题讨论】:

    标签: java spring junit easymock


    【解决方案1】:

    参数有误。

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

    【讨论】:

    • 呃!!几个小错误是如何被忽视的几个小时。感谢您指出。现在可以了。
    猜你喜欢
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多