简体   繁体   English

Mockito 使用所有匹配器验证仍然失败

[英]Mockito verify still fails on using all Matchers

I am recently writing a JUnit test and I acknowledged that Mockito either needs all Raw values or using all Matchers but I find the following case still fails with all Matchers, error message is:我最近正在写一个 JUnit 测试,我承认 Mockito 要么需要所有原始值,要么使用所有匹配器,但我发现以下情况对于所有匹配器仍然失败,错误消息是:

Invalid use of argument matchers, 0 matchers expected: 1 recorded:参数匹配器的使用无效,预计 0 个匹配器:记录 1 个:

I tested a little bit and it looks like something to do with using a stub method as value for the eq() Matcher.我进行了一些测试,它看起来与使用存根方法作为 eq() 匹配器的值有关。 See example below:请参见下面的示例:

I have a Class A very simple我有一个 Class 一个很简单

public class A {

    public void testMockitoMatcher(double a, String b){

    }
}

Here is my test case这是我的测试用例

import org.junit.Test;

import static org.mockito.Mockito.*;

public class SomeUnitTest {
    private A mockA = mock(A.class);

    @Test
    public void allMatchersDoesntWork() {
        Object mockObject = mock(Object.class);
        String someString = "Just some mocked String value to return";
        when(mockObject.toString()).thenReturn(someString);

        mockA.testMockitoMatcher(1312d, mockObject.toString());

        verify(mockA, times(1)).testMockitoMatcher(anyDouble(), eq(someString));    //<- This works
        verify(mockA, times(1)).testMockitoMatcher(anyDouble(), eq(mockObject.toString()));    //<- This doesn't by using stub method toString() to return the String value as param to eq()
    }
}

I also verified by using debug at 2nd verify statement that mockObject.toString() can still return the stubbed value for me.我还通过在第二个验证语句中使用调试来验证 mockObject.toString() 仍然可以为我返回存根值。 Also both verify use all Matchers, why mockito still gives me only 1 Matchers recorded instead of 2 at the 2nd verify?两个验证都使用所有匹配器,为什么 mockito 在第二次验证时仍然只给我记录了 1 个匹配器而不是 2 个匹配器?

Thanks!谢谢!

Mockito expects your tests to be in three stages. Mockito 希望您的测试分三个阶段进行。

  • You set things up for the test, including stubbing any methods that need to be stubbed (the "arrange" stage).您为测试设置了东西,包括对任何需要存根的方法进行存根(“安排”阶段)。
  • You run the actual method that you're trying to test (the "act" stage).您运行您尝试测试的实际方法(“行动”阶段)。
  • You make assertions about the output of the test, possibly including verifying which methods got called on your mocks (the "assert" stage).您对测试的 output 做出断言,可能包括验证在您的模拟中调用了哪些方法(“断言”阶段)。

You need to do these three things, in this sequence, because Mockito tracks whether you're stubbing, verifying or whatever.您需要按此顺序执行这三件事,因为 Mockito 会跟踪您是在进行存根、验证还是其他操作。 It also has its own internal stack which stores Matchers, for use in stubbing or verification.它还有自己的内部堆栈,用于存储匹配器,用于存根或验证。

In your second verify call in your example, the call to anyDouble() puts the Matcher onto the stack.在您的示例中的第二个verify调用中,对anyDouble()的调用将Matcher放入堆栈。 But this creates an invalid state for the call to mockObject.toString() .但这会为调用mockObject.toString()创建一个无效的 state 。

Don't do this.不要这样做。 Once you're ready to run your assertions and verifications, you shouldn't be making any more calls to stubbed methods.一旦准备好运行断言和验证,就不应再调用存根方法。 Keep the "arrange", "act" and "assert" stages of each test entirely separate from each other.将每个测试的“安排”、“行动”和“断言”阶段彼此完全分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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