Quantcast

Testing service with @Autowired dependency

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Testing service with @Autowired dependency

groovybayo
I am writing a test case for a service in our app, that has an @Autowired dependency on a bean configured in the resources.groovy file.

When the test boots up though, it fails with a "no matching bean of type [class name here] for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}"

I might be missing something extremely fundamental, but this beats me.

resources.groovy
===============
beans = {
   fooBean(FooImpl) {
    ....
   }
}

MyService.groovy
===============

class MyService {
      @Autowired Foo foo //autowire interface here
.......
}


I have tried 2 variations of the test, but to no avail. I hit the same error.

MyTest2.groovy
=============

@TestFor(MyService)
class MyTest2{
      void testSimple(){
           assert service.foo != null
      }
}

-- and --

MyTest.groovy
=============
@TestMixin(ServiceUnitTestMixin)
class MyTest {

    void setUp(){
        println "setup called."
        defineBeans {
            fooBean(FooImpl) {
            ....
            }
            testFor(MyService)
        }
        println "setup ended"
    }

    void testSomething(){
        assert service.foo != null
    }
}


Any clue?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Testing service with @Autowired dependency

groovybayo
Here is a solution that worked for me after multiple attempts at fiddling at it, in case anyone else runs into this.

1. Removed @Autowired from MyService, and just let grails inject it by name

class MyService {
      def fooBean
.......
}


2. Switched test case to this...

void testSomething() {
    defineBeans{ fooBean(FooImpl) }
         testFor(MyService)

    assert service.fooBean != null
    service.serviceMethod()
    }


For some reason,  moving these two lines to setUp() method didn't have any effect i.e.

defineBeans{ fooBean(FooImpl) }
 testFor(MyService)

It will be nice to be able to declare those in setUp(), so all other test cases in the class don't have to repeat it.

Here is a sample project on hub, illustrating this (mainly for my reference :))
https://github.com/berinle/grails-autowire-test.git
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Testing service with @Autowired dependency

Ian Roberts
On 24/07/2012 11:13, groovybayo wrote:
> For some reason,  moving these two lines to setUp() method didn't have any
> effect i.e.
>
> defineBeans{ fooBean(FooImpl) }
>  testFor(MyService)
>
> It will be nice to be able to declare those in setUp(), so all other test
> cases in the class don't have to repeat it.

Try adding a @Before annotation to the setUp method.  Rather than
forcing your per-test set up method to be called setUp, JUnit 4 allows
you to annotate any method with @Before (and likewise the @After
annotation does what tearDown used to do).  There's also @BeforeClass
and @AfterClass which are called once per class rather than once per test.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Testing service with @Autowired dependency

groovybayo
Doh!! Of course :) (silly me)

I realize shortly after I posted I had omitted that, but hadn't had a chance to be back at the computer to post a follow up.

Used the @Before annotation and it all works now.

Thanks.
Loading...