简体   繁体   English

Mockito inOrder.verify()使用mock作为函数参数失败

[英]Mockito inOrder.verify() fails using mock as function argument

I'm trying to write a unit test that checks if methods were invoked in an order. 我正在尝试编写一个单元测试来检查是否在一个订单中调用了方法。 To do that I'm using Mockito's inOrder.verify() like this: 要做到这一点,我正在使用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);
}

However, the second verify() fails with the following error: 但是,第二个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 (...)

If I change .executeOn(file) to .executeOn(any(File.class)) the test passes, but I want to make sure that the methods are invoked using the same argument. 如果我将.executeOn(file)更改为.executeOn(any(File.class)) ,测试会通过,但我想确保使用相同的参数调用这些方法。

Here's the class I'm testing: 这是我正在测试的课程:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM