running grails 2.1.3 tests in Intellij Idea: Bizarre error in Spock test: Cannot add Domain class [class x.y.Z]. It is not a Domain
Asked Answered
B

2

14

I am in the process of upgrading to grails 2.1.x, and need to redo some of my old-style tests.

I just added a new test to my spock Spec, and for this test I need to mock an additional Domain class.

Before this, I had:

@Mock([Event, EventType])

Now I have:

@Mock([Event, EventType, Notification])

Notification.groovy is in the same exact package and physical directory than Event and EventType (under grails-app/domain) so it definitely is a grails domain class.

When I try to run my test I get the following stack trace:

org.codehaus.groovy.grails.exceptions.GrailsConfigurationException: Cannot add Domain class [class x.y.Notification]. It is not a Domain!
    at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.addArtefact(DefaultGrailsApplication.java:911)
    at org.codehaus.groovy.grails.commons.DefaultGrailsApplication.addArtefact(DefaultGrailsApplication.java:615)
    at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomain(DomainClassUnitTestMixin.groovy:131)
    at grails.test.mixin.domain.DomainClassUnitTestMixin.mockDomain(DomainClassUnitTestMixin.groovy:128)

When I go through the rabbit hole and start debugging the execution, I get to this portion of DomainClassArtefactHandler (line 87):

            // make sure the identify and version field exist
            testClass.getDeclaredField(GrailsDomainClassProperty.IDENTITY);
            testClass.getDeclaredField(GrailsDomainClassProperty.VERSION);

this blows up with an exception, because I guess the id field is not present

So something is going wrong, the GORM fields are not added before this is executed.

Does anyone have a suggestion of what I need to do? Do I need to mark my domain class as an @Entity (this actually makes my test pass)?

I am pretty sure that's not supposed to be mandatory for my unit test to pass.

Any advice?

UPDATE: Actually, I have just discovered that this issue only arises when I run the unit tests inside my IDE: intellij Idea 12.1

Issues created, with test app here:

http://jira.grails.org/browse/GRAILS-9989

http://youtrack.jetbrains.com/issue/IDEA-105087

When I remove the static mapping blocks in both domain classes, the test passes!

Broadcaster answered 8/4, 2013 at 8:5 Comment(8)
Is this in the integration test folder or the unit test folder? Also if you have any spock integration tests extending specification and using @TestFor you will get really odd errors you still need to use the integrationspec for Integration tests.Seizing
Hi Jeff, it's a unit test. There are no Specs yet in the integration folder, only junit style tests. Thanks for taking interest though, I will keep this in mind.Bicknell
For that domain object is there a static mapping block?Seizing
Yes there is a static mapping block!!Bicknell
The plot thickens: I have added an updateBicknell
From your comment on the ticket for idea it seems like it is all good now, it was some odd chaching issue.Seizing
Yes, thanks Jeff! I am curious though, why you asked about static mapping blocks. Is there an issue related to them?Bicknell
Just wondering if there was some odd case with a bad id mapping.Seizing
B
21

It seems like the issue was due to IDEA not cleaning up correctly from one test run to another. The tests pass once you Rebuild the project.

Broadcaster answered 11/4, 2013 at 7:32 Comment(5)
Can you clarify what you meant by rebuild the project? I am stuck with this issue as well, its definitely IntelliJ related, but not sure what exactly need to be done to reset it.Dunker
If you are using the latest version of IDEA (14.x), there is another bug that causes the same behaviour, and rebuilding the project won't work. See youtrack.jetbrains.com/issue/IDEA-138275 and there is a workaround in the comments: disable in process compilation in settings: -Dgroovyc.in.process=false.Bicknell
ok i am trying this option, do i need to do something else, cause i am still seenig the issue?Dunker
Sorry @Dunker I have nothing more. Maybe create a support ticket on Youtrack?Bicknell
I find you need to run <strong>grails clean</strong>, reasonably often to prevent this.Bib
L
0

I had a similar issue too. All I needed was to refresh gradle project.

  1. Navigate to the Gradle tan in IntelliJ.
  2. You can click the refresh icon or right click on the project and select 'Refresh Gradle Project'.

Re run the test in IntelliJ and the error should go away.

Livre answered 16/8, 2019 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.