如何解决@MockBean 正确注入但在测试类上具有空值
我正在使用 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");
}
}
当我在@Given 方法上使用断点运行/调试我的测试时
我得到了这种奇怪的行为,正确注入了 @Mockbean
/@SpyBean
但在测试类中它的值为 null
!!我无法运行 Mockito 函数 verify
或 when
但是当我在没有黄瓜的情况下进行测试时
@Test
void fooTest() {
System.out.println("Foo Test");
}
效果很好!!值不是 null
解决方法
因此,创建了模拟 bean,但不会注入到测试套件中。我想您应该同时添加 @Autowired
和 @MockBean
注释。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。