Can I use spring to autowire controller in cucumber test?

前端 未结 3 1113
孤街浪徒
孤街浪徒 2021-02-01 22:54

I am using Cucumber to automate the testing of services and controllers in my app. In addition, I am using the Cucumber Junit runner @RunWith(Cucumber.class) in the

3条回答
  •  名媛妹妹
    2021-02-01 23:47

    I used cucumber-jvm to autowire Spring into Cucumber.

    
            info.cukes
            cucumber-core
            1.1.5
            test
        
        
            info.cukes
            cucumber-spring
            1.1.5
            test
        
        
            info.cukes
            cucumber-junit
            1.1.5
            test
        
    

    And import applicationContext.xml into Cucumber.xml

       // refer to appContext in test package
       // scan package IT and StepDefs class
    

    In CucumberIT.java call @RunWith(Cucumber.class) and add the CucumberOptions.

    @RunWith(Cucumber.class)
    @CucumberOptions(
        format = "pretty",
        tags = {"~@Ignore"},
        features = "src/test/resources/com/me/pos/service/it/"  //refer to Feature file
    )
    public class CucumberIT {  }
    

    In CucumberStepDefs.java you can @Autowired Spring

    @ContextConfiguration("classpath:cucumber.xml")
    public class CucumberStepDefs {
    
        @Autowired
        SpringDAO dao;
    
        @Autowired
        SpringService service;
    }
    

提交回复
热议问题