网上有很多方法可以将 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 {
}
脚步
@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 {
MockitoAnnotations.initMocks(this); //doesnt work with or without this line
Mockito.when(mockedBean.findByField("value"))
.thenReturn(Arrays.asList());
}
//Given-When-Then
}