黄瓜+ TestNG + SpringBoot

时间:2016-03-12 11:51:40

标签: spring-boot testng integration-testing cucumber-java

跑步者班:

@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没有自动装配,为什么?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

Cucumber和TestNG都是测试框架。 Cucumber是用于BDD测试的工具,TestNG用于单元/集成测试。我认为尝试将Cucumber与TestNG集成是不合理的。这就像将JUnit与TestNG集成一样。

因此,如果您想使用Spring执行Cucumber测试,则会有模块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() {
         //...
    }
}

运行主类RunTests后,它会尝试查找Gherkin文件并将其与步骤匹配。无关紧要如何跨Java类构建步骤。

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

答案 1 :(得分:0)

为了将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);
    }
}