Mockito验证仍然无法使用所有匹配器

发布于 2025-01-29 08:29:36 字数 1253 浏览 1 评论 0原文

我最近正在编写JUNIT测试,我承认Mockito要么需要所有原始值或使用所有匹配项,但我发现以下情况仍然失败,所有匹配项都失败了,错误消息是:

对参数匹配者的使用无效! 0个匹配项预期,1个记录:

我进行了一些测试,这似乎与使用存根方法作为eq()匹配器的值有关。请参阅下面的示例:

我有一个非常简单的

public class A {

    public void testMockitoMatcher(double a, String b){

    }
}

这是我的测试用例

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()
    }
}

我还通过在第二个验证语句上使用debug验证了opobobject.tostring()仍然可以返回对我来说是个价值。另外,两者都验证使用所有匹配器,为什么Mockito仍然只给我1个匹配者,而不是在第二个验证中录制2个匹配器?

谢谢!

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:

Invalid use of argument matchers!
0 matchers expected, 1 recorded:

I tested a little bit and it looks like something to do with using a stub method as value for the eq() Matcher. See example below:

I have a Class A very simple

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. Also both verify use all Matchers, why mockito still gives me only 1 Matchers recorded instead of 2 at the 2nd verify?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榆西 2025-02-05 08:29:36

Mockito希望您的测试处于三个阶段。

  • 您可以为测试设置内容,包括固执的任何需要固定的方法(“安排”阶段)。
  • 您运行您要测试的实际方法(“ ACT”阶段)。
  • 您就测试的输出做出断言,可能包括验证您的模拟方法被调用了哪些方法(“断言”阶段)。

您需要按照此顺序进行这三件事,因为Mockito跟踪您是否正在固执,验证或其他任何内容。它还具有自己的内部堆栈,可存储匹配器,用于固定或验证。

在您的第二个验证在您的示例中调用anydouble()Matcher调用。但这为调用mockobject.tostring()创建了无效的状态。

不要这样做。一旦准备好进行断言和验证,您就不应该再拨打任何对固定方法的电话了。将每个测试的“安排”,“ ACT”和“断言”阶段完全彼此分开。

Mockito expects your tests to be in three stages.

  • 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).

You need to do these three things, in this sequence, because Mockito tracks whether you're stubbing, verifying or whatever. 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. But this creates an invalid state for the call to mockObject.toString().

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文