2015-11-16 53 views
-1

我使用JMock測試以下方法在ProcessingTest類:JMock的期待無效方法被調用

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

我已經嘲笑了processorprocess類。對於我的測試對於這種特殊的方法,我JMock預期如下:

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

這導致以下錯誤:

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

我認爲有東西在預期不正確,他們應該是? 我曾試圖期待一次調用atLeast的方法,但這似乎是void方法的一個問題。

+0

約'驗證(處理器).handleProcess(過程)什麼;'? – Manu

+0

也許與Mockito會更容易? – Rufi

+0

@Manu它不允許我使用'verify'作爲void arguments – dres

回答

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>