Can I use spring to autowire controller in cucumber test?

前端 未结 3 1188
孤街浪徒
孤街浪徒 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:34

    In addition to Jewel's answer above do not forget to add in your own additional spring dependencies as "cucumber-spring" artifact seems to only provided the "Glue" between Cucumber and Spring.

    In my case I got Spring Injection to work in my step definitions with the following added dependency.

    
      org.springframework
      spring-test
      test
    
    
      org.springframework
      spring-core
    
    
      org.springframework
      spring-beans
    
    
      org.springframework
      spring-context
    
    
      org.slf4j
      slf4j-api
    
    
      org.slf4j
      jcl-over-slf4j
    
    
      ch.qos.logback
      logback-classic
      test
    
    ..... Plus the rest of the cucumber dependencies
    

    My Step Definition:

    @ContextConfiguration("/cucumber.xml")
    @PropertySource("classpath*:qa-test.properties")
    public class StepsDefinition {
    
        @Autowired
        private ServiceToInject serviceToInject;
    }
    

    CukesRunner

    @RunWith(Cucumber.class)
    @CucumberOptions(format = { "pretty", "html:target/cucumber-html-report", "json:target/cucumber-json-report.json" })
    public class CukesRunner {}
    

    Cucumber.xml

    
    
    
      
    
    

    applicationContext.xml

    
    
    
      
    
        
        
        
        ---- rest of my beans
      
    

提交回复
热议问题