我是 jmockit 的新手,尽管该框架目前在我们的应用程序中略微使用。
我正在尝试为我的服务层模拟我的 DAO。我了解如何使用我的期望返回来为我的读取方法返回我的对象,但我想捕获使用 create 方法创建的对象,以便我可以测试它们。
我该怎么做呢?
例如,DAO 包含:
public void create(Person person){
//code to create person here.
}
我想对其进行模拟,以便捕捉进入此方法的人,以便稍后在测试中进行询问。
根据反馈,我使用以下方法进行了测试...
@Mocked @NonStrict
private PaymentStubDAO mockPaymentStubDAO;
...
new Expectations() {
{
mockPaymentStubDAO.create((PaymentStub) any);
returns(any);
}
};
...
//set up the verifications
new Verifications() {{
mockPaymentStubDAO.create((PaymentStub) any);
forEachInvocation = new Object() {
@SuppressWarnings("unused")
void validate(PaymentStub stub) {
assertEquals(1, (int)stub.getDaysJuror());
}
};
}};
当我运行它时,我在 new Verifications() 行中收到以下错误:
java.lang.AssertionError: Missing 1 invocation to:
Object com.acs.gs.juror.dao.accounting.PaymentStubDAO#create(Object)
with arguments: null
on mock instance: $Impl_PaymentStubDAO@424f8ad5
at com.acs.gs.juror.manager.accounting.impl.AccountingManagerImplTest$2.<init>(AccountingManagerImplTest.java:1925)
at com.acs.gs.juror.manager.accounting.impl.AccountingManagerImplTest.testPayOverUnderLimit(AccountingManagerImplTest.java:1924)
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 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)
Caused by: Missing invocations
at com.acs.gs.juror.manager.accounting.impl.AccountingManagerImplTest$1.<init>(AccountingManagerImplTest.java:1859)
at com.acs.gs.juror.manager.accounting.impl.AccountingManagerImplTest.testPayOverUnderLimit(AccountingManagerImplTest.java:1845)
... 12 more