2

跑者类:

@CucumberOptions(plugin = "json:target/cucumber-report.json")
@Test(groups = "slow")
public class RunCukesTest extends AbstractTestNGCucumberTests {
}

步骤定义类:

@ContextConfiguration(loader=SpringApplicationContextLoader.class,  classes = {Application.class,MarketDataServiceMockConfig.class})
@WebIntegrationTest({"server.port=0", "management.port=0"})
@TestExecutionListeners(inheritListeners = false, listeners = {
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class })
public class MyStepdefs extends AbstractTestNGCucumberTests {

private MockMvc mockMvc;
@Autowired
private FilterChainProxy filterChainProxy;


@Autowired
private WebApplicationContext wac;

@Before
public void setUp() {

    wac = (EmbeddedWebApplicationContext) SpringApplication.run(Application.class);


}

}

filterChainProxy 和 wac 没有自动装配,为什么?

谢谢你的帮助。

4

2 回答 2

1

为了将 Cucumber 和 Spring Test 与 TestNG 而不是 JUnit 一起使用,您需要做的就是AbstractTestNGSpringContextTests在步骤定义中进行扩展,其余代码保持不变。


完整代码:

pom.xml

<!-- TestNG -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>${testng.version}</version>
    <scope>test</scope>
</dependency>

<!-- Cucumber with TestNG -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>${cucumber.version}</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- Spring Test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
    <scope>test</scope>
</dependency>

<!-- Cucumber with Spring -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>${cucumber.version}</version>
    <scope>test</scope>
</dependency>

测试执行器

@CucumberOptions(plugin = "json:target/cucumber-report.json")
@Test(groups = "slow")
public class RunCukesTest extends AbstractTestNGCucumberTests {
}

步骤定义

@ContextConfiguration(loader=SpringApplicationContextLoader.class,  classes = {Application.class,MarketDataServiceMockConfig.class})
@WebIntegrationTest({"server.port=0", "management.port=0"})
@TestExecutionListeners(inheritListeners = false, listeners = {
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class })
// Notice that the class extends AbstractTestNGSpringContextTests now
public class MyStepdefs extends AbstractTestNGSpringContextTests {

    private MockMvc mockMvc;

    @Autowired
    private FilterChainProxy filterChainProxy;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setUp() {
        wac = (EmbeddedWebApplicationContext) SpringApplication.run(Application.class);
    }
}
于 2016-09-26T13:53:55.793 回答
0

Cucumber 和 TestNG 都是测试框架。Cucumber 是用于 BDD 测试的工具,TestNG 用于单元/集成测试。我认为尝试将 Cucumber 与 TestNG 集成是没有意义的。这就像将 JUnit 与 TestNG 集成。

所以如果你想用 Spring 执行 Cucumber 测试,有 module cucumber-spring

您将拥有类似于主要测试执行程序的内容:

import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(format={"pretty", "html:target/cucumber"})
public class RunTests {
}

您将在定义 Spring 上下文的类中定义步骤:

@ContextConfiguration(YourContext.class)
public class StepDefinitions {
    @Autowired
    private YourBean yourBean;

    @Given("^your given line$")
    public void givenLine() {
         //...
    }

    @When("^your when line$")
    public void whenLine() {
         //...
    }

    @Then("^your then assertions$")
    public void thenAssertions() {
         //...
    }
}

运行 main class 后RunTests,它将尝试查找 Gherkin 文件并与步骤匹配。跨 Java 类的步骤如何构建并不重要。

但我必须警告你,Cucumber 最初是用 Ruby 设计的,所以一些概念对于 Java 程序员来说可能看起来很奇怪。步骤之间的共享状态需要通过一些类级别的变量来完成。不允许重复步骤。如果需要在两个不同类中定义的步骤之间共享状态,则需要使用一些全局对象。

于 2016-03-13T16:35:55.673 回答