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