views:

1095

answers:

1

hi everyone, i am using spring from scala and i am facing a problem when trying to inject a service with a trait/superclass.

This is my code:

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

}

This works:

@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()  

}

The problem is that myService is null in my testcases. When looking at the bytecode level (class file) all annotations are present. Any Ideas?

+2  A: 

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

Robert Munteanu
i am using the junit runner. added it to my code.
MrWhite