2013-06-29 45 views
0

我試圖編寫一個單元測試,檢查是否按順序調用方法。要做到這一點我使用的Mockito的inOrder.verify()這樣的:Mockito inOrder.verify()使用模擬作爲函數參數失敗

@Test 
public void shouldExecuteAllFileCommandsOnAFileInFIFOOrder() { 
    // Given 
    ProcessFileListCommand command = new ProcessFileListCommand(); 

    FileCommand fileCommand1 = mock(FileCommand.class, "fileCommand1"); 
    command.addCommand(fileCommand1); 

    FileCommand fileCommand2 = mock(FileCommand.class, "fileCommand2"); 
    command.addCommand(fileCommand2); 

    File file = mock(File.class, "file"); 
    File[] fileArray = new File[] { file }; 

    // When 
    command.executeOn(fileArray); 

    // Then 
    InOrder inOrder = Mockito.inOrder(fileCommand1, fileCommand2); 
    inOrder.verify(fileCommand1).executeOn(file); 
    inOrder.verify(fileCommand2).executeOn(file); 
} 

但是,第二驗證()失敗,出現以下錯誤:

org.mockito.exceptions.verification.VerificationInOrderFailure: 
Verification in order failure 
Wanted but not invoked: 
fileCommand2.executeOn(file); 
-> at (...) 
Wanted anywhere AFTER following interaction: 
fileCommand1.executeOn(file); 
-> at (...) 

如果我改變.executeOn(file).executeOn(any(File.class))的測試通過,但我想確保使用相同的參數調用方法。

下面是我測試的類:

public class ProcessFileListCommand implements FileListCommand { 

    private List<FileCommand> commands = new ArrayList<FileCommand>(); 

    public void addCommand(final FileCommand command) { 
     this.commands.add(command); 
    } 

    @Override 
    public void executeOn(final File[] files) { 
     for (File file : files) { 
      for (FileCommand command : commands) { 
       file = command.executeOn(file); 
      } 
     } 
    } 
} 

回答

2

測試失敗,因爲參數第二executeOn()方法調用是不一樣的文件作爲第一個參數,因爲第一個文件被替換由另一個在

file = command.executeOn(file); 
+0

謝謝,我不能相信我錯過了那個:D – Obszczymucha

相關問題