当使用带有黄瓜的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
也没工作
1 回答
固定
在pom.xml中
在UsersSteps类中