简体   繁体   English

JMock期望调用void方法

[英]JMock expecting void method to be invoked

I am using JMock to test the following method in a ProcessingTest class: 我正在使用JMock在ProcessingTest类中测试以下方法:

public void handle(Process process) {
    processor.handleProcess(process);
}

I have mocked out the processor and process classes. 我已经嘲笑了processorprocess类。 For my test for this particular method, my JMock expectations are as follows: 对于我对此特定方法的测试,我对JMock期望如下:

checking( new Expectations() {
         {
            oneOf( ProcessingTest.this.processor ).handleProcess(
                  ProcessingTest.this.process );
         }
      } );

This is causing the following error: 这导致以下错误:

unexpected invocation ...
no expectations specified
....

I assume that there is something incorrect in the expectations, what should they be? 我认为期望中有不正确的东西,它们应该是什么? I have tried to expect that the method in invoked atLeast one time, but this seems to be an issue for void methods. 我试图期望一次调用atLeast的方法,但这似乎是void方法的问题。

I have no idea where is your problem exactly as you have not provided enough code. 我不知道您的问题在哪里,因为您没有提供足够的代码。 This is how to do this with JMock: 这是使用JMock的方法:

import org.jmock.Expectations;
import org.jmock.integration.junit4.JUnitRuleMockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Rule;
import org.junit.Test;

public class ProcessingTest {

    @Rule
    public final JUnitRuleMockery mockery = new JUnitRuleMockery() {{
        setImposteriser(ClassImposteriser.INSTANCE);
    }};

    // just some dummy object, we will be comparing reference only
    private final Process process = mockery.mock(Process.class);

    private final Processor processor = mockery.mock(Processor.class);

    private final Processing processing = new Processing(processor);

    @Test
    public void test() {
        mockery.checking(new Expectations() {{
            oneOf(ProcessingTest.this.processor).handleProcess(ProcessingTest.this.process);
        }});

        processing.handle(process);
    }

}

public class Processing {

    private final Processor processor;

    public Processing(Processor processor) {
        this.processor = processor;
    }

    public void handle(Process process) {
        processor.handleProcess(process);
    }

}

public interface Processor {

    void handleProcess(Process process);

}

You need these dependencies: 您需要以下依赖项:

<dependency>
    <groupId>org.jmock</groupId>
    <artifactId>jmock</artifactId>
    <version>2.6.0</version>
</dependency>
<dependency>
    <groupId>org.jmock</groupId>
    <artifactId>jmock-legacy</artifactId>
    <version>2.6.0</version>
</dependency>
<dependency>
    <groupId>org.jmock</groupId>
    <artifactId>jmock-junit4</artifactId>
    <version>2.6.0</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM