سؤال

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