1

我是 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
4

2 回答 2

2

根据文档:

 @Test
public void verifyExpectationWithArgumentValidatorForEachInvocation(
     final Collaborator  mock)
{
  // Inside tested code:
  new Collaborator().doSomething(true, new int[2], "test");

  new Verifications() {{
     mock.doSomething(anyBoolean, null, null);
     forEachInvocation = new Object()
     {
        void validate(Boolean b, int[] i, String s)
        {
           assertTrue(b);
           assertEquals(2, i.length);
           assertEquals("test", s);
        }
     };
  }};

}

JMockit 文档中找到

仅供参考,正如我在您之前的问题中提到的那样,我认为 Mockito 让这变得容易多了。问问自己,你是否真的被 JMockit 锁定了。

于 2012-06-06T15:00:00.543 回答
1

除了returns(any)冗余期望块中的行之外,我在示例测试代码片段中看不到任何问题。不过,查看引发错误的完整示例测试会有所帮助。

在任何情况下,以下示例测试也应该有效:

@Test
public void createAPaymentStub(@Mocked final PaymentStubDAO mockPaymentStubDAO)
{
    // Executed somewhere inside the code under test:
    PaymentStub stub = new PaymentStub();
    stub.setDaysJuror(1);
    mockPaymentStubDAO.create(stub);

    new Verifications() {{
        mockPaymentStubDAO.create(with(new Delegate<PaymentStub>() {
           void validate(PaymentStub stub) {
               assertEquals(1, (int)stub.getDaysJuror());
           }
        }));
    }};
}

any/anyXyz字段仅用于记录/验证期望中的参数匹配器returns(...),而仅用于记录非void方法的返回值。)

或者,您可以使用 Mockups API,在这种情况下它更简单一些:

@Test
public void createAPaymentStub()
{
    PaymentStubDAO mockDAO = new MockUp<PaymentStubDAO>() {
        @Mock
        void create(PaymentStub stub) {
           assertEquals(1, (int)stub.getDaysJuror());
        }
    }.getMockInstance();

    // Executed somewhere inside the code under test:
    PaymentStub stub = new PaymentStub();
    stub.setDaysJuror(1);
    mockDAO.create(stub);
}
于 2012-06-06T17:51:35.840 回答