gpt4 book ai didi

java - Mockito inOrder.verify() 失败 - "Wanted but not invoked ... wanted anywhere AFTER following interaction"

转载 作者:行者123 更新时间:2023-11-30 06:25:11 28 4
gpt4 key购买 nike

我进行了一系列测试来开发一个简单的游戏,并且我已经达到了 Mockito 的 InOrder.verify() 产生错误的程度。

Verification in order failure. Wanted but not invoked: jumpListener.jumpPerformed( ... ) at line x. Wanted anywhere AFTER the following interaction: walkListener.walkPerformed( ... ) at line y.

我得到的代码看起来与下面看到的类似,其中相关函数 performWalk(int x)performJump(int y) 都包含 if 中的其他函数调用/else 逻辑,在 walkListeners.get(0).walkPerformed(x);jumpListeners.get(0).jumpPerformed(y); 之前和之后。在到达这一部分之前,我从未听说过 Mockito,而且大多数问题似乎是由那些生成测试的人提出的,而不是使用它们的。所以我的问题是,函数调用的 inOrder.verify() 到底需要什么?

order.verify(walkListener).walkPerformed(5);
order.verify(jumpListener).jumpPerformed(2);

这两个语句之间是否可能存在其他函数调用,例如检查参数值间隔的 boolean 函数?

public interface WalkListener extends EventListener {
public void walkPerformed(int x);
}

public interface JumpListener extends EventListener {
public void jumpPerformed(int y);
}

public class Game {
private Vector<WalkListener> walkListeners;
private Vector<JumpListener> jumpListeners;

public void addWalkListener(WalkListener listener) { ... }
public void addJumpListener(JumpListener listener) { ... }

public void performWalk(int x) {
...
walkListeners.get(0).walkPerformed(x);
...
}
public void performJump(int y) {
...
jumpListeners.get(0).jumpPerformed(y);
...
}

private class Walk implements WalkListener { ... }
private class Jump implements JumpListener { ... }
}

public class GameTest {
@Test
public void finalTest() {
Game game = new Game();
WalkListener walkListener = mock(WalkListener.class)
JumpListener jumpListener = mock(JumpListener.class)

game.addWalkListener(walkListener);
game.addJumpListener(jumpListener);

game.performWalk(5);
game.performJump(2);

InOrder order = inOrder(walkListener, jumpListener);
order.verify(walkListener).walkPerformed(5);
order.verify(jumpListener).jumpPerformed(2);
}
}

最佳答案

所以这是相关的部分:

InOrder order = inOrder(walkListener, jumpListener);
order.verify(walkListener).walkPerformed(5);
order.verify(jumpListener).jumpPerformed(2);

它验证了

  1. jumpListener 在 walkListener 之后调用(这种情况你失败了,因为你显然根本没有调用 JumpListener),
  2. walkListener.walkPerformed() 使用参数 5
  3. 调用
  4. jumpListener.jumpPerformed() 使用参数 2 进行调用

Can there be other function calls going off between these two statements, say a boolean function checking the argument value interval?

当然。

参见示例:

class Foo {
public void foo() {}
public void bar() {}
public void baz() {}
}

@Test
public void testFoo() {

Foo mock = mock(Foo.class);
mock.foo(); //1st
mock.bar(); //2nd
mock.baz(); //3rd

InOrder inOrder = inOrder(mock);

inOrder.verify(mock).foo(); //1st
inOrder.verify(mock).baz(); //3rd (last method)

//passes because there are no more interactions after last method:
inOrder.verifyNoMoreInteractions();

}

测试通过,因为在 baz() 调用之后没有更多的方法调用。 bar() 调用位于其间并不重要,因为这不是验证的一部分。

两个类的情况也是如此:

class Foo {
public void foo() {}
public void bar() {}
}

class Bar {
public void foo() {}
}

@Test
public void testFoo() {

Foo mock = mock(Foo.class);
Bar mock2 = mock(Bar.class);
mock.foo(); //1st
mock.bar(); //2nd
mock2.foo(); //3rd

InOrder inOrder = inOrder(mock, mock2);

inOrder.verify(mock).foo(); //1st
inOrder.verify(mock2).foo(); //3rd (last method)

//passes because there are no more interactions after last method:
inOrder.verifyNoMoreInteractions();
}

测试再次通过,因为第二个方法调用不是验证的一部分。

如果您希望测试在非指定方法调用上失败,则需要在测试末尾添加例如 Mockito.verifyNoMoreInteractions(mock);

关于java - Mockito inOrder.verify() 失败 - "Wanted but not invoked ... wanted anywhere AFTER following interaction",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47315350/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com