Java Mockito inoorder.verify()使用mock作为函数参数失败
我试图编写一个单元测试,检查方法是否按顺序调用。为此,我使用Mockito的inoder.verify()如下所示:Java Mockito inoorder.verify()使用mock作为函数参数失败,java,unit-testing,mockito,verify,Java,Unit Testing,Mockito,Verify,我试图编写一个单元测试,检查方法是否按顺序调用。为此,我使用Mockito的inoder.verify()如下所示: @Test public void shouldExecuteAllFileCommandsOnAFileInFIFOOrder() { // Given ProcessFileListCommand command = new ProcessFileListCommand(); FileCommand fileCommand1 = mock(FileC
@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);
}
}
}
}
公共类ProcessFileListCommand实现FileListCommand{
private List commands=new ArrayList();
public void addCommand(最终文件命令){
this.commands.add(command);
}
@凌驾
public void executeOn(最终文件[]文件){
用于(文件:文件){
用于(文件命令:命令){
file=command.executeOn(文件);
}
}
}
}
测试失败,因为第二个executeOn()
方法调用的参数与第一个方法调用的参数不是同一个文件,因为在
file = command.executeOn(file);
谢谢你,我真不敢相信我错过了那一个:D