@MockBean 注入正确,但测试类上有 null 值

You*_*sef 2 java spring cucumber mockito cucumber-java

我正在使用 Cucumber 和 Spring Boot 进行测试

@CucumberContextConfiguration
@ActiveProfiles(profiles = { "cucumber" })
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ExtendWith(SpringExtension.class)
public class FooTest {

    @Autowired
    BatchService batchService;

    @MockBean
    S3ClientService s3ClientService;

    @MockBean
    HttpClientService httpClientService;

    @SpyBean
    UndueService undueService;


    @Given("^foo cucumber test$")
    public void foo_cucumber_test() {
        System.out.println("Foo Test");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在 @Given 方法上使用断点运行/调试测试时

在此输入图像描述

我得到了这个奇怪的行为,@Mockbean/@SpyBean被正确注入,但在测试类中它的值是null!我无法运行 Mockito 函数verifywhen

但是当我在没有黄瓜的情况下进行测试时

@Test
void fooTest() {
    System.out.println("Foo Test");
}
Run Code Online (Sandbox Code Playgroud)

效果很好!这些值不是null

Sem*_*kov 6

因此,模拟 bean 已创建但未注入到测试套件中。我想你应该同时添加@Autowired@MockBean注释。

这是一个类似的问题:Spring @MockBean not located with Cucumber