Grails Unit test bug in Idea X

I was attempting to run integration and unti tests on a grails service class.
Problem was that I kept getting a NullPointerException becuase the service was not being injected into the unit and integration tests.


Hope this helps!

6 comments
Comment actions Permalink

Grails normally doesn't inject services into unit tests.

0
Comment actions Permalink

Well, for some reason the following code works on the command line and in IntelliJ 9.0.4, but not in the latests version of IDEA X.

package qotd

import grails.test.*

class QuoteServiceIntegrationTests extends GrailsUnitTestCase {
    def quoteService

    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testSomething() {
       def quote = quoteService.getStaticQuote()
      println quote.author

    }  
}

0
Comment actions Permalink

Grails should inject services for classes under test/integration, but I think those typically extend GroovyTestCase, not GrailsUnitTestCase.  That's working for me in 98.402.  Maybe GrailsUnitTestCase is interfering with your real services?  I would also ask, where are the test classes in question, and how are you running them in IntelliJ?

0
Comment actions Permalink

If memory serves (went back to a previous version of IntellIJ) my test case was in test/integration.
The test case was generated by grails when I ran 'grails create-integration-test QuoteServiceIntegration'. So, my initial assumption is that extending GrailsUnitTestCase shouldn't be an issue since it is working in my current version of IntelliJ.

When I run the test class in IntelliJ i just do a right click and hit run from the context menu. I'll give IdeaX another go - maybe I just had something buggered up.

Thanks

update:

Just installed IntelliJ X IU 98.402 imported my existing project with my tests. The test resides in test/integration as I mentioned above. When I right click the test class and click the Run > QuoteServiceIntegration option I get a NullPointerException.


java.lang.NullPointerException: Cannot invoke method getStaticQuote() on null object
 at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)
 at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
 at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)




But, when I run the exact same test case in Idea IU 9.0.4 it passes.
0
Comment actions Permalink

IntelliJ does seem flakey here.  I just changed my integration test to extend GrailsUnitTestCase.  Looking at the right-click menu in the method in the editor or on the class or method name in the Grails View, sometimes the run test icon is just the green right-triangle, sometimes it's the Grails icon with the JUnit red and green triangles superimposed, and sometimes it's a sub-menu with both the JUnit icon and the Grails icon.  It's generating both Grails and JUnit type run configurations.  Only the Grails configurations are injecting the services.

The contents of an item's context menu sometimes changes after it's run.  This looks like a bug in IntelliJ.  Now that you mention it, I've seen this problem for quite a while now.

0
Comment actions Permalink

Playing with it a little more, it seems like I get the run sub-menu with the choice between Grails and JUnit when I haven't run that test class or method before.  After I run it once, IntelliJ creates a run configuration for it, of the type I chose.  After that, run just has the green right-triangle instead of the sub-menu, until I delete that run configuration.  If I run methods individually, then I still need to choose for them, even though I've already chosen for the class.

So, this doesn't look like a bug in IntelliJ after all.  Can you try deleting your run configuration for that test (Run > Edit Configurations) and then see if you get a sub-menu choice of Grails?

0

Please sign in to leave a comment.