before 方法中的给定方法运行两次。第一次捕获 null,第二次捕获字符串参数

given method in before method runs twice. capture null for first and string argument for second time

我在 运行ning 的设置方法中先于其他方法初始化给定方法。 我只有一个测试方法,如果我 运行 这个方法,设置方法调用然后测试方法 运行s 并且没有问题。

但是当我 运行 测试 class 而不是方法时,我得到了 2 个测试 运行。一个测试得到正确的参数并通过,但另一个测试 运行s 首先给出空参数并失败。

    @Test
    @BeforeEach
    void setup(){
        given(addressService.findByEmailContaining(stringArgumentCaptor.capture())).willAnswer(invocation  -> {
.
.
.
}

    @Test
    void testArgumentSearch(){
        .
        .
        String searchAdress = controller.searchAddressForTest(address, model);
        .
        .
}

您在@BeforeEach 注释之前使用了@Test 注释。删除它,因为 JUnit 甚至将您的设置方法视为测试。