简体   繁体   English

存根和验证中的 Mockito 重复

[英]Mockito Repetition in Stubbing and Verification

It seems that in certain cases, EasyMock stubbing can take the place of verification.似乎在某些情况下,EasyMock stubbing 可以代替验证。 Take the following trivial example:举个简单的例子:

Class A: A类:

public class A {
    private B b;

    public A(B b) {
        this.b = b;
    }

    int main(int input) {
        return b.timesThree(input + 4);
    }
}

Class B: B类:

public class B {
    int timesThree(int input) {
        return input * 3;
    }
}

Class ATest: A测试类:

@ExtendWith(MockitoExtension.class)
public class ATest {
    @Mock
    B b;

    @InjectMocks
    A a;

    @Test
    void testMain() {
        doReturn(21).when(b).timesThree(7);

        int result = a.main(3);
        assertEquals(21, result);

        verify(b).timesThree(7);
    }

}

Is there a point to the verify call? verify电话有什么意义吗? The when call already asserts the input parameter to b.timesThree() is 7. when调用已经断言b.timesThree()的输入参数为 7。

In this case no.在这种情况下没有。 It comes down to what you are really trying to assert in the test.这归结为您在测试中真正想要断言的内容。

Here you are trying to test that when main method is called with a value 2 things should happen在这里,您试图测试当使用值 2 调用 main 方法时,应该会发生什么

  1. timesThree should be called with value+4应使用 value+4 调用 timesThree

  2. value returned from timesThree should be returned by your main function从 timesThree 返回的值应该由您的主函数返回

Since in your test the below three lines is enough to assert both the above cases you don't need a verify here.由于在您的测试中,以下三行足以断言上述两种情况,因此您无需在此处进行验证。

doReturn(21).when(b).timesThree(7);
int result = a.main(3);
assertEquals(21, result);

The easiest way to find out if your test is good enough is by commenting out parts of your implementation and making your test fail (TDD).确定您的测试是否足够好的最简单方法是注释掉您的部分实现并使您的测试失败 (TDD)。 As long as you get a failing test you are good.只要你的考试不及格,你就很好。

Ex: change 4 to 5 - test fails, remove input test fails例如:将 4 更改为 5 - 测试失败,删除输入测试失败

Suppose your timesThree method did not return a value, but instead stored it somewhere假设您的 timesThree 方法没有返回值,而是将其存储在某处

int main(int input) {
//Stores value somwhere
    b.timesThree(input + 4);
}

Here you test has to assert that timesThree is called once with the value 7. In this case you need a verify, since you will never be able to get a failing test without verify.在这里,您的测试必须断言 timesThree 被调用一次,值为 7。在这种情况下,您需要验证,因为如果没有验证,您将永远无法获得失败的测试。

Also on a side note one of the comments mentioned "the test would pass if the code was replaced by return 21;"同样在旁注中,其中一条评论提到“如果代码被 return 21 替换,则测试将通过;”

To proof your test against this consider using a random integer instead of hardcoding values.为了证明您对此的测试,请考虑使用随机整数而不是硬编码值。 This will make sure there is no accidental hard coding in your implementation.这将确保您的实现中没有意外的硬编码。 Example library for random intgers https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/math/RandomUtils.html#nextInt()随机整数的示例库https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/math/RandomUtils.html#nextInt()

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

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