Reputation: 156
When using Spring boot 1.4 with cucumber, @Autowired beans are not injected.
But when I use plain Junit tests, they are injected correctly! I have looked here but it doesn't solve my problem.
@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
}
Edit: Just to clarify, I did view Cucumber with Spring Boot 1.4: Dependencies not injected when using @SpringBootTest and @RunWith(SpringRunner.class) and put this annotations
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
didnt work
then I put these annotations (as in the answer)
@ContextConfiguration
@SpringBootTest
didnt work either
Upvotes: 1
Views: 696
Reputation: 156
fixed
in pom.xml
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber-junit.version}</version>
<scope>test</scope>
</dependency>
In UsersSteps class
@SpringBootTest
@ContextConfiguration(classes = {ServicesApplication.class})
@TestPropertySource(locations = "classpath:test.properties")
public class UsersSteps
Upvotes: 1