小编Rog*_*eia的帖子

如何集成 Spring Boot、Cucumber 和 Mockito?

网上有一些方法可以将 Cucumber 与 Spring Boot 集成。但我也找不到如何使用 Mockito 来做到这一点。如果我使用 Cucumber 运行程序并使用 ContextConfiguration 和 SpringBootTest 注释步骤文件,则容器会注入 Autowired 依赖项,一切都很好。问题是用 Mock、MockBean 和 InjectMocks 注释的依赖项不起作用。任何人都知道为什么它不起作用以及如何使其起作用?

编辑:可以使用mock(Bean.class)实例化bean,而不是使用Mock注释。但是像 MockBean 和 InjectMocks 这样的功能呢?

跑步者

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "html:build/cucumber_report/"},
                 features = "classpath:cucumber/",
                 glue = {"com.whatever"},
                 monochrome = true,
                 dryRun = false)
public class CucumberTest {

}
Run Code Online (Sandbox Code Playgroud)

脚步

@ContextConfiguration
@SpringBootTest
public class CucumberSteps
{

    @Autowired
    private Bean bean;

    @InjectMocks //doesnt work
    private AnotherBean anotherBean;

    @MockBean //doesnt work with @Mock also
    MockedBean mockedBean;


    @Given("^Statement$")
    public void statement() throws Throwable { …
Run Code Online (Sandbox Code Playgroud)

java cucumber mockito spring-boot spring-boot-test

5
推荐指数
1
解决办法
5683
查看次数

标签 统计

cucumber ×1

java ×1

mockito ×1

spring-boot ×1

spring-boot-test ×1