简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM