在 Cucumber 步骤中自动装配字段会产生 NullPointerException
Autowiring a field in a Cucumber step produces NullPointerException
Cucumber runner 配置:
@RunWith(Cucumber.class)
@CucumberOptions(features = "/features",glue {""})
@ContextConfiguration(classes = Init.class,
initializers = ConfigFileApplicationContextInitializer.class)
public class Test {
}
专题文件:
class feature {
Scenario: Save test in db.
When I inject a payload in the aws queue
Then it should be integrated in database
}
步骤定义文件::
@CucumberAbstract
@ContextConfiguration("classpath:application.yml")
public class MyStepdefs {
@Autowired
private QueueMessagingTemplate queueMessagingTemplate;
@Autowired
TestRepository testRepository;
String message = "Hi";
public MyStepdefs() {
When("^I inject a payload in the aws queue$", () -> {
this.queueMessagingTemplate.convertAndSend("QUEUE_NAME", message);
});
}
@Then("^it should be integrated in database$")
public void itShouldBeIntegratedInDatabase() throws Throwable {
//validations
}
}
我尝试在此处创建自定义注释并在 stepDefinition 文件之上使用此注释。
抽象测试:::
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ContextConfiguration(classes = Init.class, loader =
SpringApplicationContextLoader.class)
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public @interface CucumberAbstract {
}
Cucumber runner (cucumber.api.junit.Cucumber
) 不加载任何 Spring 上下文。
所以你的黄瓜步骤 class 没有 Spring 意识到。
要将 bean 注入到 class 的字段中,您必须加载 Spring 上下文,在您的例子中是 Spring 引导上下文。
所以指定 @SpringBootTest
就是您要查找的内容:
@SpringBootTest
public class MyStepdefs {
...
}
实际上你需要的是将 Cucumber 与 Spring Boot 集成。
所以你需要为你的maven项目添加这些依赖:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.7.4</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>4.7.4</version>
</dependency>
你的黄瓜赛跑者是一样的:
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty"}, features = {"src/main/resources"})
public class ReportCaseCucumberTest {
}
现在在您的 StepDefinition class 中,您需要配置 ContextConfiguration 才能使用 Spring。
@SpringBootTest
@ContextConfiguration(
classes = Application.class,
loader = SpringBootContextLoader.class)
public class StepsDefinitions {
@Autowired
private ApplicationContext appContext;
}
现在在您的 StepDefinition class 中,您可以使用 "Autowired" 注释来注入 bean。
这解决了我的问题,你也可以看看这个教程:
https://medium.com/@bcarunmail/set-up-and-run-cucumber-tests-in-spring-boot-application-d0c149d26220
Cucumber runner 配置:
@RunWith(Cucumber.class)
@CucumberOptions(features = "/features",glue {""})
@ContextConfiguration(classes = Init.class,
initializers = ConfigFileApplicationContextInitializer.class)
public class Test {
}
专题文件:
class feature {
Scenario: Save test in db.
When I inject a payload in the aws queue
Then it should be integrated in database
}
步骤定义文件::
@CucumberAbstract
@ContextConfiguration("classpath:application.yml")
public class MyStepdefs {
@Autowired
private QueueMessagingTemplate queueMessagingTemplate;
@Autowired
TestRepository testRepository;
String message = "Hi";
public MyStepdefs() {
When("^I inject a payload in the aws queue$", () -> {
this.queueMessagingTemplate.convertAndSend("QUEUE_NAME", message);
});
}
@Then("^it should be integrated in database$")
public void itShouldBeIntegratedInDatabase() throws Throwable {
//validations
}
}
我尝试在此处创建自定义注释并在 stepDefinition 文件之上使用此注释。 抽象测试:::
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ContextConfiguration(classes = Init.class, loader =
SpringApplicationContextLoader.class)
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public @interface CucumberAbstract {
}
Cucumber runner (cucumber.api.junit.Cucumber
) 不加载任何 Spring 上下文。
所以你的黄瓜步骤 class 没有 Spring 意识到。
要将 bean 注入到 class 的字段中,您必须加载 Spring 上下文,在您的例子中是 Spring 引导上下文。
所以指定 @SpringBootTest
就是您要查找的内容:
@SpringBootTest
public class MyStepdefs {
...
}
实际上你需要的是将 Cucumber 与 Spring Boot 集成。 所以你需要为你的maven项目添加这些依赖:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.7.4</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>4.7.4</version>
</dependency>
你的黄瓜赛跑者是一样的:
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty"}, features = {"src/main/resources"})
public class ReportCaseCucumberTest {
}
现在在您的 StepDefinition class 中,您需要配置 ContextConfiguration 才能使用 Spring。
@SpringBootTest
@ContextConfiguration(
classes = Application.class,
loader = SpringBootContextLoader.class)
public class StepsDefinitions {
@Autowired
private ApplicationContext appContext;
}
现在在您的 StepDefinition class 中,您可以使用 "Autowired" 注释来注入 bean。
这解决了我的问题,你也可以看看这个教程: https://medium.com/@bcarunmail/set-up-and-run-cucumber-tests-in-spring-boot-application-d0c149d26220