-1

I am using JMock to test the following method in a ProcessingTest class:

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

I have mocked out the processor and process classes. For my test for this particular method, my JMock expectations are as follows:

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.

kirsty
  • 267
  • 1
  • 2
  • 14
dres
  • 499
  • 6
  • 18

1 Answers1

3

I have no idea where is your problem exactly as you have not provided enough code. This is how to do this with 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>
Jaroslaw Pawlak
  • 5,538
  • 7
  • 30
  • 57