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

后端 未结 5 1607
情深已故
情深已故 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 16:57

    Prior to Spring Boot 1.4 you can use

    @ContextConfiguration(classes = {YourSpringConfiguration.class}, loader = SpringApplicationContextLoader.class)
    

    From Spring Boot 1.4 onwards SpringApplicationContextLoader is deprecated so you should use SpringBootContextLoader.class instead

    Really just adding @SpringBootTest (with an optional configuration class) should work on its own, but if you look at the code in cucumber.runtime.java.spring.SpringFactory method annotatedWithSupportedSpringRootTestAnnotations it's not checking for that annotation, which is why simply adding that annotation in conjunction with @SpringBootTest works.

    Really the code in cucumber-spring needs to change. I'll see if I can raise an issue as in the Spring docs it states that SpringApplicationContextLoader should only be used if absolutely necessary.I'll try and raise an issue for this for the cucumber spring support.

    So as it stands stripwire's answer using a combination of @SpringBootTest and @ContextConfiguration is the best workaround.

提交回复
热议问题