【问题标题】:Spring Boot 1.4 issue with Cucumber-JVMCucumber-JVM 的 Spring Boot 1.4 问题
【发布时间】:2016-08-29 02:15:46
【问题描述】:

当使用带有黄瓜的 Spring boot 1.4 时,@Autowired bean 不会被注入。

但是当我使用普通的 Junit 测试时,它们被正确注入了! 我看过here,但它并没有解决我的问题。

@SpringBootApplication
@EnableSwagger2
@ComponentScan("org.services")
public class ServicesApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServicesApplication.class, args);
    }
}


@RunWith(Cucumber.class)
public class UsersTest {

}

@RunWith(SpringRunner.class)
@SpringBootTest
public class UsersSteps {

    @Autowired
    private UsersService _target;//null
}

编辑: 澄清一下,我确实查看了Cucumber with Spring Boot 1.4: Dependencies not injected when using @SpringBootTest and @RunWith(SpringRunner.class) 并把这个注释

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)

没用

然后我把这些注释(如答案)

@ContextConfiguration
@SpringBootTest

也不行

【问题讨论】:

标签: spring-boot cucumber-jvm cucumber-junit cucumber-java


【解决方案1】:

固定

在 pom.xml 中

 <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>${cucumber-junit.version}</version>
            <scope>test</scope>
</dependency>

在 UsersSteps 类中

    @SpringBootTest
    @ContextConfiguration(classes = {ServicesApplication.class})
    @TestPropertySource(locations = "classpath:test.properties")
    public class UsersSteps 

【讨论】:

  • 这对我有用。我的场景是 cucumber 1.2.5, spring boot 1.4.4 ,并运行“spring boot cookbook”书中的示例,将原始注释更改为@SpringBootTest @ContextConfiguration(classes = {BookPubApplication.class, TestMockBeansConfig.class})
猜你喜欢
  • 2016-12-12
  • 2016-11-11
  • 1970-01-01
  • 2016-10-31
  • 2016-10-29
  • 2016-11-27
  • 2016-12-14
  • 1970-01-01
  • 2017-01-09
相关资源
最近更新 更多