运行SpringBoot应用程序,使用Cuccumber实现CommandLineRunner,在运行Feature测试用例之前运行主应用程序

时间:2017-08-18 00:50:55

标签: java spring spring-boot cucumber

我是Cucumber和Spring Boot的新手,我正在开发一个实现CommandLineRunner的Spring Boot应用程序,并尝试将其与Cucumber Framework集成以运行一些测试并创建相应的报告。 现在我的Cucumber测试用例运行正常,但在运行测试用例之前,它运行我的Springboot应用程序(Application.java)。这是预期的行为,还是仅仅是为了运行我的测试。

Main 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 
}

现在我的黄瓜课程是: -

  1. 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() {
        }
    
    }
    
  2. TestValidations: -

    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());
    }
    
  3. CucumberConfiguration .java

    @Configuration
    @ComponentScan(basePackages = "au.com.nab.mx")
    public class CucumberConfiguration {
    
    }
    
  4. 在我的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'
    }
    

    .Feature

    功能: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测试用例。现在我不确定它是否存在预期的行为或我错过了什么。

    此致 维克拉姆帕坦尼亚

2 个答案:

答案 0 :(得分:1)

使用@ContextConfiguration和@SpringBootTest将加载测试所需的应用程序上下文。

文件: http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html

答案 1 :(得分:1)

答案是 - 是的,这是预期的行为,因为使用了SpringRunner.class和@SpringBootTest以及@ContextConfiguration注释。