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

huangapple go评论84阅读模式
英文:

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

请注意,代码中的 &quot; 应该被替换为双引号 "。如果你需要更多帮助,请告诉我。

英文:

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 = &quot;select count(*) from tablename where ucid=?&quot;
		decision = jdbcTemplate.queryForObject(sql, new Object[]{ucid}, String.class);
		return (Integer.parseInt(decision)&gt;0);
	}

AuthenticationDaoImplTest.java

@RunWith(EasyMockRunner.class)
public class AuthenticationDaoImplTest {

    @TestSubject
    private AuthenticationDaoImpl authenticationDaoImpl = new AuthenticationDaoImpl();

    @Mock
    JdbcTemplate jdbcTemplateObject;

    @Test
    public void checkIfCallExistTest(){
	    String sql = &quot;select count(*) from tablename where ucid=?&quot;;
        Object[] params = new Object[] { &quot;testucid&quot; };
        EasyMock.expect(this.jdbcTemplateObject.queryForObject(sql, params, String.class)).andReturn(&quot;0&quot;);
        EasyMock.replay(this.jdbcTemplateObject);
        boolean res = this.authenticationDaoImpl.checkIfCallExist(&quot;testUcid&quot;);
        assertEquals(false, res);
    }
}

Error Stacktrace

java.lang.AssertionError: 
  Unexpected method call JdbcTemplate.queryForObject(&quot;select count(*) from tablename where ucid=?&quot;, [&quot;testUcid&quot;], class java.lang.String):
    JdbcTemplate.queryForObject(&quot;select count(*) from tablename where ucid=?&quot;, [&quot;testucid&quot;], 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(&lt;generated&gt;)
	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[] { &quot;testucid&quot; }

this.authenticationDaoImpl.checkIfCallExist(&quot;testUcid&quot;); // should be testucid

huangapple
  • 本文由 发表于 2020年8月7日 19:08:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63300578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定