我正在使用JMock
在ProcessingTest类中测试以下方法:
public void handle(Process process) {
processor.handleProcess(process);
}
我已经模拟了processor
和process
类。对于我对此特定方法的测试,我的JMock
预期如下:
checking( new Expectations() {
{
oneOf( ProcessingTest.this.processor ).handleProcess(
ProcessingTest.this.process );
}
} );
这导致以下错误:
unexpected invocation ...
no expectations specified
....
我认为期望中存在不正确之处,应该是什么?
我曾尝试过一次调用atLeast
方法,但这似乎是void
方法的一个问题。
答案 0 :(得分:3)
我不知道你的问题究竟在哪里,因为你没有提供足够的代码。这是如何使用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);
}
您需要这些依赖项:
<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>