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

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

我正在使用 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");
    }
}

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

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

@Test
void fooTest() {
    System.out.println("Foo Test");
}

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

arknldoa

arknldoa1#

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

相关问题