我在Spock框架中编写规范类。
@ContextConfiguration(classes = [MyServiceImplementation.class])
class MyServiceSpecification extends Specification {
@Autowired
private MyService myServiceImplementation
def " " {
//code
}
}
该类MyServiceImplementation
带有注释@Service
。我没有使用XML配置。MyServiceImpl
是接口的实现:MyService
。
为什么自动装配对象为myServiceImplementation
空?
我尝试使用ComponentScan
,但仍然无法正常工作。
首先,你需要有两个spock-core
和spock-spring
在类路径中。其次,@ContextConfiguration(classes=
获取配置类而不是bean类的列表。
@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();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句