首页 > 解决方案 > 使用 Cucumber 时,在嵌入式 Cassandra 之前加载 Spring Boot,我该如何解决这个问题?

问题描述

我在使用 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();
     }
...

}

关于如何解决这个问题的任何建议?

标签: javaspring-bootcassandracucumber

解决方案


我就是这样做的:

集成配置:

class IntegrationConfiguration {
// your cassandra startup
}

组件测试规范

@ContextConfiguration(classes = Application.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@Import(IntegrationConfiguration.class)
abstract class ComponentTestSpecification {
// reusable integration-test methods here
}

测试(groovy,应该可以转换为 jUnit / 不管):

class AccessControllerSpec extends ComponentTestSpecification {
// test methods
}

推荐阅读