Grails Spock integration test example?

5,853 views
Skip to first unread message

David Hay

unread,
Apr 30, 2012, 1:14:53 PM4/30/12
to spockfr...@googlegroups.com
Hi,
 
Using Grails Spock plugin for 2.0.3.  Unit tests are running fine, but struggling to set up integration tests.
 
I believe I should extend IntegrationSpec, correct?
 
Is anything autowired for me?  for example if I need a service, do I just declare it, or do I need to new it up?
 
Also, what about integration testing controllers, so I can test the whole stack?  How do I access the request and add a param to it?
 
Many thanks
 
David
 
 

Luke Daley

unread,
May 1, 2012, 10:05:24 PM5/1/12
to spockfr...@googlegroups.com
On 30/04/2012, at 10:14 AM, David Hay wrote:

 
Using Grails Spock plugin for 2.0.3.  Unit tests are running fine, but struggling to set up integration tests.
 
I believe I should extend IntegrationSpec, correct?

If you need auto wiring and data isolation, yes.

Is anything autowired for me?  for example if I need a service, do I just declare it, or do I need to new it up? 

Yes, just declare it.

Also, what about integration testing controllers, so I can test the whole stack?  How do I access the request and add a param to it?


It's the same, except you extend IntegrationSpec. I'm sorry I don't have more complete examples for you on hand.

David Hay

unread,
May 2, 2012, 11:17:06 AM5/2/12
to spockfr...@googlegroups.com
Hi Luke,
 
Thanks for the feedback!  More questions inline...

On Tue, May 1, 2012 at 10:05 PM, Luke Daley <lda...@gmail.com> wrote:

On 30/04/2012, at 10:14 AM, David Hay wrote:

 
Using Grails Spock plugin for 2.0.3.  Unit tests are running fine, but struggling to set up integration tests.
 
I believe I should extend IntegrationSpec, correct?

If you need auto wiring and data isolation, yes.

Is anything autowired for me?  for example if I need a service, do I just declare it, or do I need to new it up? 

Yes, just declare it.
Great - I since got it working well for a spock integration test - the service is autowired beautifully :)
 

Also, what about integration testing controllers, so I can test the whole stack?  How do I access the request and add a param to it?

See the testing controllers section of http://grails.org/doc/latest/guide/testing.html#integrationTesting

It's the same, except you extend IntegrationSpec. I'm sorry I don't have more complete examples for you on hand.
Here's where I am stills struggling.  I created a TestController, and a TestControllerSpec.groovy under test/integration:
 
package ccs

grails.plugin.spock.IntegrationSpec;

class

TestControllerSpec extends IntegrationSpec {

   def

testController

   def setUp = {

       println controller

       println

testController

   }

   def "test controller exists" () {

      setup:

      println

testController

      when:

      1 == 1

      then:

      true

   }

}

However, both controller and testController are always null.

I'm at a loss - this should work, right?  Can you see anything I'm missing?  Can you point me to where to start to debug this?

thanks,

David

Anto Aravinth

unread,
May 2, 2012, 11:24:59 AM5/2/12
to spockfr...@googlegroups.com
You should create a controller like this :


def yourController = new yourController()


Then add the params and other required into the created controller variable like this : 

yourController.params.something = "your params details"


Hope it helps!

David Hay

unread,
May 2, 2012, 3:09:26 PM5/2/12
to spockfr...@googlegroups.com
That works.  But I'm still confused as to why the controller is not autowired.
 
any ideas?

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To post to this group, send email to spockfr...@googlegroups.com.
To unsubscribe from this group, send email to spockframewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.

Jonathan Stott

unread,
May 3, 2012, 4:01:42 AM5/3/12
to spockfr...@googlegroups.com
I think you need to put the @TestFor(TestController) annotation on the class to get the autowired controller instance. We haven't had any problems getting a reference to the controller.

Jonathan
--
Dr Jonathan Stott
Development Team Leader

Axon Limited
"Connecting Healthcare"
2 Venture Road
University of Southampton Science Park
Southampton SO16 7NP


Tel: +44 (0)2380 11 11 99
Mobile: +44(0)7950 467087

www.axonuk.com

This message and the information contained therein is intended only for the use of the person(s) to whom it is addressed.  It may contain information that is confidential or privileged within the meaning of applicable law.  If you are not the intended recipient, please contact the sender as soon as possible and delete this message from your system; please also note that any use or disclosure of the information contained in this message (including any attachments) is strictly prohibited and may be unlawful.  This correspondence may include examples or terms based upon current assumptions with any costs shown excluding VAT and cannot be considered as a quotation, offer or commitment in any way.  Whilst reasonable precaution has been taken to minimise the risk, the contents or an attachment to this e-mail may have become corrupted during transmission or contain viruses, we cannot accept liability in this regard, and you should carry out your own virus checks.

Axon Limited is a Company registered in England and Wales number 5728502. Registered Office: Imperial House, 18-21 Kings Park Road, Southampton SO15 2AT

David Hay

unread,
May 4, 2012, 4:51:49 PM5/4/12
to spockfr...@googlegroups.com
Thanks, Jonathan.
 
So, I added the @TestFor(TestController), and I got the following error:
 
| Running 1 spock test...
| Failure:  ccs.TestControllerSpec
|  java.lang.NullPointerException: Cannot get property 'autowireCapableBeanFactory' on null object
        at grails.plugin.spock.IntegrationSpec.setupSpec(IntegrationSpec.groovy:47)
| Failure:  ccs.TestControllerSpec
|  java.lang.NullPointerException: Cannot invoke method isActive() on null object
        at grails.test.mixin.support.GrailsUnitTestMixin.shutdownApplicationContext(GrailsUnitTestMixin.groovy:232)
        at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
        at org.spockframework.runtime.extension.builtin.JUnitFixtureMethodsExtension$FixtureType$FixtureMethodInterceptor.intercept(JUnitFixtureMethod
sExtension.java:145)
        at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
        at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
 
This is an *integration* test - I believe @TestFor is only for unit tests?
 
Are you able to get the Controller injected in an Integration test which extends IntegrationSpec?
 
thanks,
 
David

Sebastian Gozin

unread,
May 6, 2013, 4:37:28 AM5/6/13
to spockfr...@googlegroups.com
I'd find it odd to add @TestFor annotations on an integration test.
As for obtaining an instance of a controller remember that Grails uses property name matching to decide if it should inject a dependency. As I have no idea what name Grails registers the controllers with I have always simply injected the grailsApplication bean and then used that to call grailsApplication.context.getBean(MyController)

- Sebastian


On 06 May 2013, at 10:11, Kim Betti <kim....@gmail.com> wrote:

Did you get anywhere with this? 

I'm having the same problem. 
To unsubscribe from this group and stop receiving emails from it, send an email to spockframewor...@googlegroups.com.

To post to this group, send email to spockfr...@googlegroups.com.

Abhijith

unread,
May 6, 2013, 1:01:05 PM5/6/13
to spockfr...@googlegroups.com
Never put @TestFor on a Integration test. Even though it is not explicitly
mentioned in Grails documentation @TestFor annotation is for unit test cases
only.

I found it odd too that DI does not work for Controller. But, at this
moment I have done
def controller = new MyController()

Cheers,
Abhijith



--
View this message in context: http://spock-framework.3207229.n2.nabble.com/Grails-Spock-integration-test-example-tp7514236p7573219.html
Sent from the Spock Framework mailing list archive at Nabble.com.
Reply all
Reply to author
Forward
0 new messages