@mockbean已正确注入,但测试类上的值为空

raogr8fs  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(812)

我正在使用 cucumber 和Spring Boot测试

  1. @CucumberContextConfiguration
  2. @ActiveProfiles(profiles = { "cucumber" })
  3. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
  4. @ExtendWith(SpringExtension.class)
  5. public class FooTest {
  6. @Autowired
  7. BatchService batchService;
  8. @MockBean
  9. S3ClientService s3ClientService;
  10. @MockBean
  11. HttpClientService httpClientService;
  12. @SpyBean
  13. UndueService undueService;
  14. @Given("^foo cucumber test$")
  15. public void foo_cucumber_test() {
  16. System.out.println("Foo Test");
  17. }
  18. }

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

我有一种奇怪的行为 @Mockbean / @SpyBean 正确注入,但在测试类中其值为 null !! 我不能运行mockito函数 verifywhen 但是当我在没有 cucumber 的情况下进行测试时

  1. @Test
  2. void fooTest() {
  3. System.out.println("Foo Test");
  4. }

它很好用!!这些值不正确 null

arknldoa

arknldoa1#

因此,将创建模拟bean,但不会将其注入测试套件。我想你应该把两者都放进去 @Autowired@MockBean 注解。
这里有一个类似的问题:spring@mockbean没有注射 cucumber

相关问题