4

我正在使用来自 scala 的 spring,并且在尝试注入具有特征/超类的服务时遇到了问题。

这是我的代码:

trait MyServiceHolder{
  var myService:MyService = null

  @Autowired
  def setMyService(ms:MyService) = myService = ms
}

@RunWith(classOf[SpringJUnit4ClassRunner])
@ContextConfiguration(Array("file:src/main/webapp/WEB-INF/application-context.xml"))
class MyConcreteClass extends MyServiceHolder{

  def hello() = myService.hello()  

}

这有效:

@RunWith(classOf[SpringJUnit4ClassRunner])
@ContextConfiguration(Array("file:src/main/webapp/WEB-INF/application-context.xml"))
class MyConcreteClass{

  var myService:MyService = null

  @Autowired
  def setMyService(ms:MyService) = myService = ms

  def hello() = myService.hello()  

}

问题是 myService 在我的测试用例中为空。查看字节码级别(类文件)时,所有注释都存在。有任何想法吗?

4

1 回答 1

3

You need to use a form of the Spring TestContext Framework to have your beans configured by Spring when running tests.

于 2009-06-02T11:11:00.467 回答