Ali*_*yuy 13 java cucumber-jvm spring-boot
我正在编写一个新应用程序并尝试使用黄瓜和Spring Boot 1.4进行BDD.工作代码如下所示:
@SpringBootApplication
public class Application {
@Bean
MyService myService() {
return new MyService();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
public class MyService {}
Run Code Online (Sandbox Code Playgroud)
测试代码如下所示:
@RunWith(Cucumber.class)
public class RunFeatures {}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
public class MyStepDef {
@Autowired
MyService myService;
@Given("^Some initial condition$")
public void appIsStarted() throws Throwable {
if (service == null) throw new Exception("Dependency not injected!");
System.out.println("App started");
}
@Then("^Nothing happens$")
public void thereShouldBeNoException() throws Throwable {
System.out.println("Test passed");
}
}
Run Code Online (Sandbox Code Playgroud)
功能文件如下所示:
Feature: Test Cucumber with spring
Scenario: First Scenario
Given Some initial condition
Then Nothing happens
Run Code Online (Sandbox Code Playgroud)
当我按原样运行上面的操作时,一切正常,并且依赖(MyService)被注入MyStepDef而没有任何问题.
如果我替换此代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
Run Code Online (Sandbox Code Playgroud)
使用下面的代码(在Spring Boot 1.4中处理它的新方法):
@RunWith(SpringRunner.class)
@SpringBootTest
Run Code Online (Sandbox Code Playgroud)
然后依赖(MyService)永远不会被注入.我也许错过了什么?
在此先感谢您的帮助!!!
str*_*ire 17
我有同样的问题.上面的评论指导我解决方案
黄瓜弹簧中有问题的代码似乎是这个github.com/cucumber/cucumber-jvm/blob/master/spring/src/main/...
添加注释后@ContextConfiguration,测试按预期工作.
所以我得到的是以下......
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"json:target/cucumber.json", "pretty"}, features = "src/test/features")
public class CucumberTest {
}
@ContextConfiguration
@SpringBootTest
public abstract class StepDefs {
}
public class MyStepDefs extends StepDefs {
@Inject
Service service;
@Inject
Repository repository;
[...]
}
Run Code Online (Sandbox Code Playgroud)
我希望这会对你有所帮助
我将其与Spring Boot 1.5.x和2.0配合使用,然后写了一篇博客文章试图澄清这一点,因为这很棘手。
首先,即使很明显,您也需要在项目中包含正确的依赖项(在cucumber-spring这里很重要)。例如,使用Maven:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
现在,使它起作用的重要部分总结如下:
@RunWith(Cucumber.class。@Given,@When,@Then等等)。@SpringBootTest,@RunWith(SpringRunner.class)以及使用Spring Boot运行测试所需的任何其他配置。例如,如果要实现集成测试而不模拟其他层,则应添加webEnvironment配置并将其设置为RANDOM_PORT或DEFINED_PORT。请参见下面的图和代码框架。
入口点:
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features/bag.feature", plugin = {"pretty", "html:target/cucumber"})
public class BagCucumberIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)
Spring Boot基础测试类:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class SpringBootBaseIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)
步骤定义类:
@Ignore
public class BagCucumberStepDefinitions extends SpringBootBaseIntegrationTest {
// @Given, @When, @Then annotated methods
}
Run Code Online (Sandbox Code Playgroud)
这就是使DI工作所需的功能。对于完整的代码示例,只需检查我的博客文章或GitHub中的代码即可。
| 归档时间: |
|
| 查看次数: |
6399 次 |
| 最近记录: |