I have 1 bean defined in resources.groovy

cachedbean(serviceImpl) {
  }

In service I am using it this way

    MyService{

      static transactional = false
      def cachedbean

        myMeth(){
         cachedbean.get("cacheKey")
      }
   }

This works fine but when I try to test it with integration test, I get nullpointer exception on 'get' cachedbean.get("cacheKey").

How does it work?

有帮助吗?

解决方案

Instead of "new"-ing up the service instance, if you let Grails auto wire the service bean into your integration test class it should be auto wired.

class MyServiceTests extends GroovyTestCase {
    def myService
    void testSomething () {
        // myService should already be wired up
    }
}

其他提示

  1. It's unclear if you did or not based on your sample code, but you need to fully-qualify out the package and class name for "serviceImpl" in your resources.groovy unless you explicitly import the package.

  2. You may need to add inside the scope of your declaration in resources.groovy the line bean.autowire = "byName"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top