[groovy-user] Groovy/grails unit testing with IntelliJ IDEA: java.lang.NullPointerException: Cannot invoke method methodName()

53 views
Skip to first unread message

ahoegh

unread,
Aug 4, 2009, 5:41:36 AM8/4/09
to us...@groovy.codehaus.org

Hi,

Comming from an ASP.NET MVC background, im currently trying to get a hold of
the capabilities of Groovy/Grails and it seems very promissing - but I
encounter a problem trying to do my first tests using IntelliJ IDEA 8.1.3.

The problem seems to be that when I define a variable with a name like
"quoteService" in my unit test, the relationship to "QuoteService" is not
inferred and thus, I get a java.lang.NullPointerException for trying to run
a method on a null object. In my QuoteController, however, I can easily
define a variable named "quoteService" and have Grails sort out the
relationship to the "QuoteService". (Note: I base my examples on the "Grails
In Action" book which I find usefull for those of us familar with Java
syntax, domain-driven design, the MVC pattern and so on..)

I suppose this is a configuration issue, but I have been searching the
internet for some time now and I can't figure how to set this up with Grails
Test or NUnit. Furthermore, I've noticed that a ".grails" folder is created
under my "Administrator" user folder in Windows and some test classes are
stored there, although my project folder is on the C:\ root.

I "projectfolder\grails-app\services":

class QuoteService {

boolean transactional = false

def getRandomQuote()
{
def quotesList = Quote.list()
def quoteRandom
def quoteCount = quotesList.size()

if (quotesList.size() > 0)
{
def randomIdx = new Random().nextInt(quotesList.size())
quoteRandom = quotesList[randomIdx]
}

return quoteRandom;
}
}

In "projectfolder\test\unit":

import grails.test.*

class QuoteServiceTests extends GrailsUnitTestCase {

def quoteService

void testgetRandomQuote()
{
def randomQuote = quoteService.getRandomQuote()
assertNotNull(randomQuote)
}

}

The stack trace when running the test(s):


java.lang.NullPointerException: Cannot invoke method getRandomQuote() on
null object
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)
at
org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:750)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:727)
at
org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:17)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:121)
at
QuoteServiceIntegrationTests.testRandomQuote(QuoteServiceIntegrationTests.groovy:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)


Process finished with exit code -1


Any help will be appreciated.
--
View this message in context: http://www.nabble.com/Groovy-grails-unit-testing-with-IntelliJ-IDEA%3A-java.lang.NullPointerException%3A-Cannot-invoke-method-methodName%28%29-tp24805364p24805364.html
Sent from the groovy - user mailing list archive at Nabble.com.


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

http://xircles.codehaus.org/manage_email


Roger Studner

unread,
Aug 4, 2009, 7:44:36 AM8/4/09
to us...@groovy.codehaus.org
I believe if you are inside the "unit" folder for your test.. then you
do not get any dependency injection.

Put the test beneath 'integration' and you'll have better luck.

Note, running 'tests' from inside intellij is *very slow* for
integration tests.. as well it will throw some exceptions.. just wait
them out, it will eventually work.

Roger

Reply all
Reply to author
Forward
0 new messages