[英]Mockito: InOrder.verify does not prohibit intermediate calls on a mock
[英]Mockito inOrder.verify() fails using mock as function argument
我正在嘗試編寫一個單元測試來檢查是否在一個訂單中調用了方法。 要做到這一點,我正在使用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);
}
但是,第二個verify()失敗並出現以下錯誤:
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);
}
}
}
}
測試失敗,因為第二個executeOn()
方法調用的參數與第一個參數的參數不同,因為第一個文件被另一個文件替換。
file = command.executeOn(file);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.