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!
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.