我正在尝试匹配我在 Dao 课程中使用的这种方法。但是我总是得到以下异常,表明没有对该方法进行调用。
要么方法不匹配,要么我做错了什么。
String pSql = "SELECT * FROM employee";
Object[] pArgs = new Object[] {""};
int[] pArgTypes = new int[] {};
/* Return 1 when the call to update() is made indicating a successful database update */
when(mJdbcTemplate.update(anyString(), aryEq(pArgs), aryEq(pArgTypes))).thenReturn(1);
这是异常的堆栈跟踪:
Wanted but not invoked:
jdbcTemplate.update(<any>, <any>, <any>);
-> at com.test.GenericDaoJdbcImplTest$WhenInsertUpdateDeleteIsCalledWith.successfulUpdateShouldReturnTrue(GenericDaoJdbcImplTest.java:197)
However, there were other interactions with this mock:
-> at com.test.GenericDaoJdbcImplTest.insertUpdateDelete(GenericDaoJdbcImpl.java:121)
at org.mockito.exceptions.Reporter.wantedButNotInvoked(Reporter.java:269)
at org.mockito.internal.verification.checkers.MissingInvocationChecker.check(MissingInvocationChecker.java:42)
at org.mockito.internal.verification.Times.verify(Times.java:36)
at org.mockito.internal.verification.MockAwareVerificationMode.verify(MockAwareVerificationMode.java:21)
at org.mockito.internal.MockHandler.handle(MockHandler.java:80)
at org.mockito.internal.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:36)
at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:48)
at org.springframework.jdbc.core.JdbcTemplate$$EnhancerByMockitoWithCGLIB$$92326890.update(<generated>)
at com.test.GenericDaoJdbcImplTestTest$WhenInsertUpdateDeleteIsCalledWith.successfulUpdateShouldReturnTrue(GenericDaoJdbcImplTest.java:197)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
MyGenericDaoJdbcImplTest
是一个抽象类。这是我正在测试的课程。
public abstract class GenericDaoJdbcImpl<MODEL, PRIMARYKEY extends Serializable>
implements GenericJdbcDao<MODEL, PRIMARYKEY> {
@Autowired
@Qualifier(value = "jdbcTemplate")
private JdbcTemplate mJdbcTemplate;
private Class<MODEL> mType;
public JdbcTemplate getJdbcTemplate() {
return mJdbcTemplate;
}
public void setJdbcTemplate(final JdbcTemplate pJdbcTemplate) {
this.mJdbcTemplate = pJdbcTemplate;
}
public GenericDaoJdbcImpl(final Class<MODEL> pType) {
this.mType = pType;
}
public abstract MODEL add(final MODEL mModel);
public abstract MODEL modify(final MODEL mModel);
public abstract MODEL read(PRIMARYKEY pId);
public abstract List<MODEL> list();
public abstract void delete(PRIMARYKEY pId);
@Override
public boolean insertUpdateDelete(final String pSql, final Object[] pArgs,
final int[] pArgTypes) {
Assert.hasLength(pSql, "No SQL provided to execute");
Assert.notNull(pArgs, "No data provided to insert/update/delete");
Assert.notNull(pArgTypes, "No data types provided for");
Assert.isTrue(pArgs.length == pArgTypes.length, "Mis-match in data and data type count");
return (mJdbcTemplate.update(pSql, pArgs, pArgTypes) > 0);
}
}