Cucumber with Spring Boot 1.4: Dependencies not injected when using @SpringBootTest and @RunWith(SpringRunner.class)

后端 未结 5 1601
情深已故
情深已故 2021-02-07 16:43

I am writing a new app and trying to do BDD using cucumber and Spring Boot 1.4. Working code is as shown below:

@SpringBootApplication
public class Application {         


        
5条回答
  •  隐瞒了意图╮
    2021-02-07 17:13

    I've got it working in Spring Boot 1.5. I want to share the configuration with you:

    pom.xml

    
    
            org.springframework.boot
            spring-boot-starter-parent
            1.5.7.RELEASE
             
        
    
            ...
        
                ...
            
                info.cukes
                cucumber-java
                1.2.5
                test
            
            
                info.cukes
                cucumber-spring
                1.2.5
                test
            
            
                info.cukes
                cucumber-junit
                1.2.5
                test
            
        
            ...
    
    

    Feature file

    Feature: User should be greeted
    
      Background:
        Given The database is empty
        Then All connections are set
    
      Scenario: Default user is greeted
        Given A default user
        When The application is started
        Then The user should be greeted with "Hello Marc!"
    

    Cucumber hook

    @RunWith(Cucumber.class)
    @CucumberOptions(features = "src/test/resources", strict = true)
    public class CucumberTests {    // Classname should end on *Tests
    
    }
    

    Abstract Spring Configuration

    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    @ContextConfiguration
    abstract class AbstractSpringConfigurationTest {
    
    }
    

    Glue

    class CucumberGlue : AbstractSpringConfigurationTest() {
    
        @Autowired
        lateinit var restTemplate: TestRestTemplate
    
        @Autowired
        lateinit var restController: RestController
    
        @Autowired
        lateinit var personRepository: PersonRepository
    
        @Autowired
        lateinit var entityManager: EntityManager
    
        private var result: String? = null
    
        @Given("^The database is empty$")
        fun the_database_is_empty() {
            personRepository.deleteAll()
        }
    
        @Then("^All connections are set$")
        fun all_connections_are_set() {
            assertThat(restTemplate).isNotNull()
            assertThat(entityManager).isNotNull()
        }
    
        @Given("^A default user$")
        fun a_default_user() {
        }
    
        @When("^The application is started$")
        fun the_application_is_started() {
            result = restController.testGet()
        }
    
        @Then("^The user should be greeted with \"([^\"]*)\"$")
        fun the_user_should_be_greeted_with(expectedName: String) {
            assertThat(result).isEqualTo(expectedName)
        }
    
    }
    

提交回复
热议问题