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();
}
}