I'm writing a specification class in spock framework.
@ContextConfiguration(classes = [MyServiceImplementation.class])
class MyServiceSpecification extends Specification {
@Autowired
private MyService myServiceImplementation
def " " {
//code
}
}
The class MyServiceImplementation is annotated @Service. I'm not using XML configuration. MyServiceImpl is an implementation of the interface: MyService.
Why is the autowired object myServiceImplementation null?
I tried using ComponentScan and it still didn't work.
First, you need to have both spock-core and spock-spring on the classpath. Second, @ContextConfiguration(classes= takes a list of configuration classes, not bean classes.
@ContextConfiguration(classes = [MyConfig.class])
class MyServiceSpecification extends Specification {
@Autowired
private MyService myServiceImplementation
def " " {
//code
}
}
// You could also define @ComponentScan here
class MyConfig {
@Bean
MyService myService() {
return new MyServiceImplementation();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With