Can I use spring to autowire controller in cucumber test?

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

    I just made cucumber and spring working with java based configuration and I think it is worth to share here. here what I have done:

    @Configuration
    @ComponentScan(basePackages = { "com.*" })
    @PropertySource("classpath:application-${environment}.properties")
    public class AppConfiguration {
    
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    
    }
    }
    

    my step defintion class:

    @ContextConfiguration(classes= AppConfiguration.class)
    public class Stepdefs {
    @Autowired
    private MyBean mybean;
    

    and here the test class:

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

    and the maven command is:

    mvn -Denvironment=dev clean test
    

    the maven dependencies I have in my pom are:

    
                org.springframework
                spring-expression
                ${org.springframework.version}
            
    
            
                org.springframework
                spring-beans
                ${org.springframework.version}
            
            
                org.springframework
                spring-aop
                ${org.springframework.version}
            
            
                org.springframework
                spring-context
                ${org.springframework.version}
            
            
                org.springframework
                spring-context-support
                ${org.springframework.version}
            
    
            
            
                org.springframework
                spring-test
                ${org.springframework.version}
            
    
            
                junit
                junit
                ${junit.version}
                test
            
    
            
                org.hamcrest
                hamcrest-library
                ${hamcrest.version}
                test
            
    
            
            
                info.cukes
                cucumber-java
                ${cucumber.version}
                test
            
            
                info.cukes
                cucumber-spring
                ${cucumber.version}
                test
            
    
            
                info.cukes
                cucumber-junit
                ${cucumber.version}
                test
            
    
            
                info.cukes
                cucumber-jvm
                ${cucumber.version}
                pom
            
    

提交回复
热议问题