如何解决Spring Boot Cucumber Java 8 测试 ApplicationOnReady 事件
我有一个报告应用程序,可以生成关于 ApplicationReadyEvent 的报告。我正在尝试为它编写黄瓜测试,但是由于应用程序事件甚至在我的功能执行之前就被触发,我不确定什么是测试它的正确方法。我可以在测试期间控制事件吗?
@EventListener(ApplicationReadyEvent.class)
private void generateAccuracyAnalysisReport() throws IOException
{
//some Logic
}
黄瓜类:
@SpringBootTest
@CucumberContextConfiguration
@ActiveProfiles("junit")
public class CucumberConfiguration
{
}
@RunWith(Cucumber.class)
@CucumberOptions(plugin = "pretty",features = "src/test/resources/cucumber/features")
public class CucumberFullIntegrationTest
{
}
步骤定义:
public class ReportStepDefs implements En {
public ReportStepDefs() {
When("^System sends an application event to generate report$",() -> {
});
Then("^Report should be generated successfully\\.$",() -> {
});
}
}
解决方法
如果您的 Cucumber 测试涉及 Spring 生命周期,则不能使用 cucumber-spring
。相反,作为每个场景的一部分,您必须使用 Springs ApplicationContextRunner
之类的东西来配置、运行和验证有关您的应用程序的某些内容。
// Given
ApplicationContextRunner contextRunner = new ApplicationContextRunner();
// When
contextRunner.withConfiguration(AutoConfigurations.of(...);
// Then
contextRunner.run(context -> assertThat(context).... /* something */ );
// Or assert something external to the application context.
虽然听起来您的应用程序正在做某事然后退出。如果是这样,您应该在 Web 应用程序中使用 CommandLineRunner
而不是 ApplicationReadyEvent
。这可以用 cucumber-spring
测试。
@RequiredArsConstructor
public class StepDefinitions {
final MyCommandLineRunner commandLineRunner;
@When(....)
public void something() {
commandLineRunner.run("input.txt","input2.txt");
}
@Then(....)
public void assertSomething() {
// check if report was generated
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。