Grails

NPE in unit tests using MockUtils

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Blocker Blocker
  • Resolution: Fixed
  • Affects Version/s: 1.3.3
  • Fix Version/s: 1.3.4
  • Component/s: Testing
  • Labels:
    None
  • Testcase included:
    yes

Description

After upgrading from Grails 1.3.2 to 1.3.3, lots of unit tests fail with stack traces like these:

java.lang.NullPointerException
at grails.test.MockUtils.mockDomain(MockUtils.groovy:443)
at grails.test.MockUtils$mockDomain.call(Unknown Source)
at grails.test.GrailsUnitTestCase.mockDomain(GrailsUnitTestCase.groovy:131)
at grails.test.GrailsUnitTestCase.mockDomain(GrailsUnitTestCase.groovy:129)

Sample project from Ben Schreur:

svn checkout http://agiletrackingtool.googlecode.com/svn/trunk/ agiletrackingtool-read-onlysvncheckout

Activity

Hide
Paul Woods added a comment -

execute the tests to see the npe.

Show
Paul Woods added a comment - execute the tests to see the npe.
Hide
Paul Woods added a comment -

I also have the same problem.
In unit tests, when the domain is mocked, and .save() is called, I get a NPE. See the attached file for a project.

java.lang.NullPointerException
at grails.test.MockUtils$_addValidateMethod_closure83.doCall(MockUtils.groovy:973)
at grails.test.MockUtils$_addValidateMethod_closure84.doCall(MockUtils.groovy:1014)
at grails.test.MockUtils$_addDynamicInstanceMethods_closure67.doCall(MockUtils.groovy:736)
at grails.test.MockUtils$_addDynamicInstanceMethods_closure67.doCall(MockUtils.groovy)
at test2.NameTests.testSaving(NameTests.groovy:19)
at TestApp$_run_closure4.doCall(TestApp:268)
at TestApp$_run_closure4.call(TestApp)
at TestApp$_run_closure2.doCall(TestApp:225)
at TestApp$_run_closure1_closure21.doCall(TestApp:184)
at TestApp$_run_closure1.doCall(TestApp:171)
at TestApp$_run_closure1.doCall(TestApp.groovy:101)
at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:427)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:415)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.executeTargets(Gant.groovy:590)
at gant.Gant.executeTargets(Gant.groovy:589)

Show
Paul Woods added a comment - I also have the same problem. In unit tests, when the domain is mocked, and .save() is called, I get a NPE. See the attached file for a project. java.lang.NullPointerException at grails.test.MockUtils$_addValidateMethod_closure83.doCall(MockUtils.groovy:973) at grails.test.MockUtils$_addValidateMethod_closure84.doCall(MockUtils.groovy:1014) at grails.test.MockUtils$_addDynamicInstanceMethods_closure67.doCall(MockUtils.groovy:736) at grails.test.MockUtils$_addDynamicInstanceMethods_closure67.doCall(MockUtils.groovy) at test2.NameTests.testSaving(NameTests.groovy:19) at TestApp$_run_closure4.doCall(TestApp:268) at TestApp$_run_closure4.call(TestApp) at TestApp$_run_closure2.doCall(TestApp:225) at TestApp$_run_closure1_closure21.doCall(TestApp:184) at TestApp$_run_closure1.doCall(TestApp:171) at TestApp$_run_closure1.doCall(TestApp.groovy:101) at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381) at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415) at gant.Gant$_dispatch_closure7.doCall(Gant.groovy) at gant.Gant.withBuildListeners(Gant.groovy:427) at gant.Gant.this$2$withBuildListeners(Gant.groovy) at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source) at gant.Gant.dispatch(Gant.groovy:415) at gant.Gant.this$2$dispatch(Gant.groovy) at gant.Gant.invokeMethod(Gant.groovy) at gant.Gant.executeTargets(Gant.groovy:590) at gant.Gant.executeTargets(Gant.groovy:589)
Hide
Victor Benarbia added a comment -

Same issue,
All my unit tests who try to run constraints tests are dead.

Example:

Stacktrace

java.lang.NullPointerException
at grails.test.MockUtils$_addValidateMethod_closure83.doCall(MockUtils.groovy:973)
at grails.test.MockUtils$_addValidateMethod_closure84.doCall(MockUtils.groovy:1014)
at com.arm.nimbus.ProductUnitTests.testConstraints(ProductUnitTests.groovy:26)

Standard Output

-Output from testConstraints-
-Output from testPaths-

Standard Error

-Output from testConstraints-
-Output from testPaths-

Show
Victor Benarbia added a comment - Same issue, All my unit tests who try to run constraints tests are dead. Example: Stacktrace java.lang.NullPointerException at grails.test.MockUtils$_addValidateMethod_closure83.doCall(MockUtils.groovy:973) at grails.test.MockUtils$_addValidateMethod_closure84.doCall(MockUtils.groovy:1014) at com.arm.nimbus.ProductUnitTests.testConstraints(ProductUnitTests.groovy:26) Standard Output -Output from testConstraints- -Output from testPaths- Standard Error -Output from testConstraints- -Output from testPaths-
Hide
Burt Beckwith added a comment -

You can work around the issue by registering a fake plugin manager. Add these two imports:

import org.codehaus.groovy.grails.plugins.GrailsPluginManager
import org.codehaus.groovy.grails.plugins.PluginManagerHolder

and these setUp() and tearDown() methods (or add to existing methods):

protected void setUp() {
   super.setUp()
   PluginManagerHolder.pluginManager = [hasGrailsPlugin: { String name -> true }] as GrailsPluginManager
}

protected void tearDown() {
   super.tearDown()
   PluginManagerHolder.pluginManager = null
}
Show
Burt Beckwith added a comment - You can work around the issue by registering a fake plugin manager. Add these two imports:
import org.codehaus.groovy.grails.plugins.GrailsPluginManager
import org.codehaus.groovy.grails.plugins.PluginManagerHolder
and these setUp() and tearDown() methods (or add to existing methods):
protected void setUp() {
   super.setUp()
   PluginManagerHolder.pluginManager = [hasGrailsPlugin: { String name -> true }] as GrailsPluginManager
}

protected void tearDown() {
   super.tearDown()
   PluginManagerHolder.pluginManager = null
}
Hide
Paul Woods added a comment -

Thanks, Burt. Your workaround works for my projects.
Paul

Show
Paul Woods added a comment - Thanks, Burt. Your workaround works for my projects. Paul
Hide
Ben Schreur added a comment -

Can also confirm that the workaround works for my project.
http://code.google.com/p/agiletrackingtool/source/detail?r=151
Thank you for the fast response!

Show
Ben Schreur added a comment - Can also confirm that the workaround works for my project. http://code.google.com/p/agiletrackingtool/source/detail?r=151 Thank you for the fast response!
Hide
Ben Schreur added a comment -

Deploying created war (grails clean; grails prod war) gives the error logging as attached. There seems to be a relation with the PluginManager.

Show
Ben Schreur added a comment - Deploying created war (grails clean; grails prod war) gives the error logging as attached. There seems to be a relation with the PluginManager.
Hide
Ben Schreur added a comment -

Using the workaround as suggested for the testcases also seems to work for running on tomcat:
http://code.google.com/p/agiletrackingtool/source/diff?spec=svn152&r=152&format=side&path=/trunk/grails-app/conf/BootStrap.groovy

Show
Ben Schreur added a comment - Using the workaround as suggested for the testcases also seems to work for running on tomcat: http://code.google.com/p/agiletrackingtool/source/diff?spec=svn152&r=152&format=side&path=/trunk/grails-app/conf/BootStrap.groovy
Hide
Matthias Hryniszak added a comment -

I can confirm that the workaround works for my test cases in 1.3.3.

Any idea when we'll see that fixed? I'm in the middle of some project and all of the sudden all my tests cases that had to do with saving domain class instances have gone wild.

Show
Matthias Hryniszak added a comment - I can confirm that the workaround works for my test cases in 1.3.3. Any idea when we'll see that fixed? I'm in the middle of some project and all of the sudden all my tests cases that had to do with saving domain class instances have gone wild.
Hide
Abdelilah JAZOULI added a comment -

I confirm that the workaround works also for me (1.3.3 version).
Thanks Burt

Show
Abdelilah JAZOULI added a comment - I confirm that the workaround works also for me (1.3.3 version). Thanks Burt
Hide
Masatoshi Hayashi added a comment -

Hello, I use Spock Framework for unit test now.
If possible, would you add the code for fix (PluginManagerHolder...) to another point? (for example, _GrailsTest.groovy but this fix is for [grails test-app] command only...)

Show
Masatoshi Hayashi added a comment - Hello, I use Spock Framework for unit test now. If possible, would you add the code for fix (PluginManagerHolder...) to another point? (for example, _GrailsTest.groovy but this fix is for [grails test-app] command only...)
Hide
Luke Daley added a comment -

I will port the fix to the Spock plugin.

Show
Luke Daley added a comment - I will port the fix to the Spock plugin.
Hide
Masatoshi Hayashi added a comment -

Thanks, then I have no problem with this fix.

Show
Masatoshi Hayashi added a comment - Thanks, then I have no problem with this fix.
Hide
Robert Fletcher added a comment -

I'm still seeing this issue using Grails 1.3.4 and Spock 0.5-groovy-1.7-SNAPSHOT. Is this really fixed?

Show
Robert Fletcher added a comment - I'm still seeing this issue using Grails 1.3.4 and Spock 0.5-groovy-1.7-SNAPSHOT. Is this really fixed?
Hide
Luke Daley added a comment -

I haven't fixed in Spock yet. Because of the way the Grails test code is structured I had to copy a lot of stuff so it's not using the same codebase.

Show
Luke Daley added a comment - I haven't fixed in Spock yet. Because of the way the Grails test code is structured I had to copy a lot of stuff so it's not using the same codebase.
Hide
Ricardo J. Méndez added a comment -

Hi Luke,

As this issue is closed, is there another one we should watch for the Spock fixes, or will you report the updates here?

Show
Ricardo J. Méndez added a comment - Hi Luke, As this issue is closed, is there another one we should watch for the Spock fixes, or will you report the updates here?
Hide
Luke Daley added a comment -

I've just fixed the spock plugin, so no need for an issue thanks.

Show
Luke Daley added a comment - I've just fixed the spock plugin, so no need for an issue thanks.

People

Vote (17)
Watch (23)

Dates

  • Created:
    Updated:
    Resolved: