Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在jmockit中模拟创建_Java_Jmockit - Fatal编程技术网

Java 如何在jmockit中模拟创建

Java 如何在jmockit中模拟创建,java,jmockit,Java,Jmockit,我对jmockit不太熟悉,尽管该框架目前在我们的应用程序中使用 我试图为我的服务层模拟我的DAO。我知道如何使用预期的返回来为我的read方法返回对象,但我想捕获使用create方法创建的对象,以便测试它们 我该怎么做 例如,DAO包含: public void create(Person person){ //code to create person here. } 我想对它进行模拟,这样我就可以捕捉到使用这种方法的人,这样我就可以在以后的测试中进行询问 根据反馈,我使用以下内容

我对jmockit不太熟悉,尽管该框架目前在我们的应用程序中使用

我试图为我的服务层模拟我的DAO。我知道如何使用预期的返回来为我的read方法返回对象,但我想捕获使用create方法创建的对象,以便测试它们

我该怎么做

例如,DAO包含:

public void create(Person person){
    //code to create person here.
}
我想对它进行模拟,这样我就可以捕捉到使用这种方法的人,这样我就可以在以后的测试中进行询问

根据反馈,我使用以下内容进行了测试

@Mocked @NonStrict
private PaymentStubDAO mockPaymentStubDAO;

}})

当我运行它时,我在带有新验证的行上得到以下错误()

java.lang.AssertionError:缺少对以下对象的1个调用:
对象com.acs.gs.juror.dao.accounting.PaymentStubDAO#create(对象)
带参数:null
在模拟实例上:$Impl_PaymentStubDAO@424f8ad5
在com.acs.gs.juror.manager.accounting.impl.AccountingManagerImplTest$2。(AccountingManagerImplTest.java:1925)
com.acs.gs.juror.manager.accounting.impl.AccountingManagerImplTest.testPayOverUnderLimit(AccountingManagerImplTest.java:1924)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于java.lang.reflect.Method.invoke(Method.java:597)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于java.lang.reflect.Method.invoke(Method.java:597)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于java.lang.reflect.Method.invoke(Method.java:597)
位于org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
位于org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
原因:缺少调用
在com.acs.gs.juror.manager.accounting.impl.AccountingManagerImplTest$1。(AccountingManagerImplTest.java:1859)
com.acs.gs.juror.manager.accounting.impl.AccountingManagerImplTest.testPayOverUnderLimit(AccountingManagerImplTest.java:1845)
... 还有12个
根据文档:

 @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);
        }
     };
  }};
}

发现于


仅供参考,正如我在上一个问题中提到的,我认为Mockito让这变得容易多了。问问自己是否真的锁定了JMockit。

除了在冗余期望块中返回(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());
           }
        }));
    }};
}

请发布一些带有create方法签名的代码和正在测试的代码。是否需要模拟create?或者您可以捕获传递给DAO以存储对象的参数吗?@JohnB我想我回答了您的问题。不需要上面的
newexpectations(){…}
块,因为您只想在测试结束时验证对
create
的调用。上面显示的验证块似乎正确,但错误消息表明
PaymentStubDAO#create
方法采用
Object
类型的参数。。。顺便问一下,使用的是哪个版本的JMockit?@Rogerio它采用参数化类型。那是交易的破坏者吗?不幸的是,我是。我已经添加了我得到的例外情况。你能看一下吗?
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
 @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);
        }
     };
  }};
@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());
           }
        }));
    }};
}
@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);
}