spring引导在使用cucumber时在嵌入cassandra之前加载,如何修复这个问题?

9wbgstp7  于 2021-06-14  发布在  Cassandra
关注(0)|答案(3)|浏览(325)

我有一些问题,使用 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();
     }
...

}

你有什么建议吗?

py49o6xq

py49o6xq1#

我就是这样做的:
集成配置:

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/whatever):

class AccessControllerSpec extends ComponentTestSpecification {
// test methods
}
rsaldnfx

rsaldnfx2#

你可以通过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` 因此,你不会有任何问题与启动。
lbsnaicq

lbsnaicq3#

目前cucumber只调用 TestContextManager.beforeClass 以及 TestContextManager.afterClass . 然而,这种情况发生在每一个场景被覆盖之前 TestExecutionListener.afterTestClass 应该会成功的。

相关问题