【发布时间】:2023-03-16 09:25:01
【问题描述】:
我是 Cucumber 和 Spring Boot 的新手,我正在开发一个实现 CommandLineRunner 的 Spring Boot 应用程序,并尝试将它与 Cucumber 框架集成以运行一些测试并创建相应的报告。 现在我的 Cucumber 测试用例运行良好,但在运行测试用例之前它运行我的 Springboot 应用程序(Application.java)。这是预期的行为还是只是为了运行我的测试。
主 Spring Boot 类 - Application.java 类:-
/**
* Main Application Class
*/
@SpringBootApplication
public class Application implements CommandLineRunner {
@Override
public void run(String... args) {
@Autowired
private GWMLController gwmlController;
@Autowired
private SmartXmlController mxMLController;
@Autowired
private ReportingController reportingController;
@Autowired
private ComparisionReportController comparisionReportController;
....
...
My busniess logic
}
现在我的黄瓜课是:-
-
AbstractDefination.java
package cucumberJava.steps; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringRunner.class) @ActiveProfiles("test") @SpringBootTest @AutoConfigureMockMvc @WebAppConfiguration public class AbstractDefinitions{ public AbstractDefinitions() { } } -
测试验证:-
import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertNotNull; @ContextConfiguration(classes = {CucumberConfiguration.class}) public class TestValidations extends AbstractDefinitions { @Autowired private GWMLController gwmlController; @Autowired private SmartXmlController mxMLController; @Autowired private ComparisionReportController comparisionReportController; @Given("^GID map is not empty$") public void guid_map_is_not_null() throws Throwable { comparisonResultMap = comparisionReportController.makeComparisionMappingMap (comparisonResultMap); assertFalse(comparisonResultMap.isEmpty()); } -
黄瓜配置.java
@Configuration @ComponentScan(basePackages = "au.com.nab.mx") public class CucumberConfiguration { }
在我的 build.gradle 中有:-
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-
test', version: '1.5.4.RELEASE'
compile group: 'net.sf.supercsv', name: 'super-csv', version: '2.4.0'
compile group: 'info.cukes', name: 'cucumber-java', version: '1.2.5'
compile group: 'info.cukes', name: 'cucumber-core', version: '1.2.5'
compile group: 'info.cukes', name: 'gherkin', version: '2.12.2'
testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.1'
testCompile group: 'info.cukes', name: 'cucumber-spring', version: '1.2.5'
testCompile group: 'info.cukes', name: 'cucumber-junit', version: '1.2.5'
}
.特征
特征:Validations.feature
Scenario Outline: COMPARE_COLUMNS
Given GID map is not empty
When <smt> not-null and <gwml> not null
Then <smt> validateEqual <gwml>
@Smoke
Examples:
| smt | gwml |
| **SMT_GUID | **GWML_GUID |
| SMT_BUY_SELL | GWML_BUY_SELL |
现在我的问题是每当我运行我的应用程序时,它首先运行我的 Application.java,然后运行我的 Cucumber 测试用例。现在我不确定它是预期的行为还是我遗漏了什么。
问候, 维克拉姆·帕塔尼亚
【问题讨论】:
-
这是预期的行为,因为测试会在测试场景之前加载 spring 的容器及其上下文,这是由于 @SpringBootTest 的存在
-
感谢 Barath 的回答。有什么方法可以避免这种情况,因为我认为我需要使用 @SpringBootTest 来加载我的上下文。
-
我们可以通过从行为角度定义场景来测试正在运行的应用程序来避免它。你所写的更像是测试控制器和服务的集成测试。而是通过对正在运行的应用程序进行 REST 调用来测试场景
标签: java spring spring-boot cucumber