我在使用spring-cassandra-unit,spring-boot和spring-cucumber时遇到一些问题。下面的配置对于单元测试很好用,但是一旦我将spring-cucumber添加到混合中并尝试进行一些集成测试,它似乎完全忽略了MyCustomOrderedTestExecutionListener并在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;
}
}
黄瓜测试运行器:
@RunWith(Cucumber.class)
@CucumberOptions(features="resources/features", glue="resources/glue")
public class TestRunner {}
编辑:
看一下黄瓜弹簧的弹簧工厂,甚至在执行beforeTestClass之前就已经加载了应用上下文(beforeTestClass由notifyContextManagerAboutTestClassStarted执行):
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();
}
...
}
关于如何解决此问题的任何建议?
答案 0 :(得分:1)
当前,黄瓜仅调用TestContextManager.beforeClass
和TestContextManager.afterClass
。但是,这是在每种情况之前发生的,因此覆盖TestExecutionListener.afterTestClass
应该可以解决问题。
答案 1 :(得分:1)
这是我的方法:
IntegrationConfig:
class IntegrationConfiguration {
// your cassandra startup
}
ComponentTestSpecification :
@ContextConfiguration(classes = Application.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@Import(IntegrationConfiguration.class)
abstract class ComponentTestSpecification {
// reusable integration-test methods here
}
测试(常规,应该可以转换为jUnit /其他):
class AccessControllerSpec extends ComponentTestSpecification {
// test methods
}
答案 2 :(得分:0)
您可以通过https://github.com/nosan/embedded-cassandra项目
执行此操作import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import com.github.nosan.embedded.cassandra.test.spring.EmbeddedCassandra;
@SpringBootTest
@ActiveProfiles("INTEGRATION_TEST")
@EmbeddedCassandra(configurationFile = "cassandra.yaml", scripts = "cql/dataset1.cql")
public class TestStepDef {
}
@EmbeddedCassandra
由org.springframework.test.context.ContextCustomizer
处理,因此启动不会有任何问题。