我有一些问题,使用 Spring Cassandra单位,Spring Boot , Spring cucumber 。下面的配置对于单元测试很好,但是当我将spring cucumber添加到混合中并尝试一些集成测试时,它似乎完全忽略了我的自定义orderedtestexecutionlistener,并在cassandra之前加载spring boot,给了我一个“nohostfoundexception”。
我真的需要一些关于如何确保首先加载嵌入式cassandra的建议。非常感谢您的帮助。
以下设置:
@ActiveProfile("INTEGRATION_TEST")
@SpringBootTest
@EmbeddedCassandra(configuration = "cassandra.yaml")
@TestExecutionListeners(
listeners = MyCustomOrderedTestExecutionListener.class,
mergeMode = MERGE_WITH_DEFAULTS)
@CassandraDataSet(value = "cql/dataset1.cql", keyspace = "mykeyspace")
public class TestStepDef{
//omitted for brevity
}
我的自定义顺序测试执行侦听器:
public class MyCustomOrderedTestExecutionListener extends AbstractTestExecutionListener {
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
//omitted for brevity
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
cucumber 试验跑者:
@RunWith(Cucumber.class)
@CucumberOptions(features="resources/features", glue="resources/glue")
public class TestRunner {}
编辑:
查看cucumber spring的spring工厂,甚至在执行ForeTestClass之前就加载了应用程序上下文(在NotifyContextManagerOutTestClassStarted执行ForeTestClass之前):
public void start() {
if (this.stepClassWithSpringContext != null) {
this.testContextManager = new CucumberTestContextManager(this.stepClassWithSpringContext);
} else if (this.beanFactory == null) {
this.beanFactory = this.createFallbackContext();
}
this.notifyContextManagerAboutTestClassStarted();
if (this.beanFactory == null || this.isNewContextCreated()) {
this.beanFactory = this.testContextManager.getBeanFactory();
Iterator var1 = this.stepClasses.iterator();
while(var1.hasNext()) {
Class<?> stepClass = (Class)var1.next();
this.registerStepClassBeanDefinition(this.beanFactory, stepClass);
}
}
GlueCodeContext.INSTANCE.start();
}
再深入一点,我们可以看到应用程序上下文加载在这里:
class CucumberTestContextManager extends TestContextManager {
public CucumberTestContextManager(Class<?> testClass) {
super(testClass);
this.registerGlueCodeScope(this.getContext());
}
private ConfigurableApplicationContext getContext() {
return (ConfigurableApplicationContext)this.getTestContext().getApplicationContext();
}
...
}
你有什么建议吗?
3条答案
按热度按时间py49o6xq1#
我就是这样做的:
集成配置:
组件测试规范:
测试(groovy,应该可以转换为junit/whatever):
rsaldnfx2#
你可以通过https://github.com/nosan/embedded-cassandra 项目
lbsnaicq3#
目前cucumber只调用
TestContextManager.beforeClass
以及TestContextManager.afterClass
. 然而,这种情况发生在每一个场景被覆盖之前TestExecutionListener.afterTestClass
应该会成功的。