简体   繁体   English

在Intellij中运行grails 2.1.3测试Idea:Spock测试中的奇怪错误:无法添加Domain类[class xyZ]。 它不是域名

[英]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. 我正在升级到grails 2.1.x,并且需要重做一些旧式测试。

I just added a new test to my spock Spec, and for this test I need to mock an additional Domain class. 我刚刚为我的spock Spec添加了一个新测试,对于这个测试,我需要模拟一个额外的Domain类。

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. Notification.groovy与Event和EventType(在grails-app / domain下)位于相同的包和物理目录中,因此它绝对是一个grails域类。

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): 当我通过兔子洞并开始调试执行时,我到达DomainClassArtefactHandler的这一部分(第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 这会引发异常,因为我猜id字段不存在

So something is going wrong, the GORM fields are not added before this is executed. 因此出现了问题,在执行此操作之前不会添加GORM字段。

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)? 我是否需要将我的域类标记为@Entity(这实际上是我的测试通过)?

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 更新:实际上,我刚刚发现只有当我在IDE中运行单元测试时才会出现这个问题:intellij Idea 12.1

Issues created, with test app here: 创建的问题,测试应用程序在这里:

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

http://youtrack.jetbrains.com/issue/IDEA-105087 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. 似乎问题是由于IDEA没有在一次测试运行中正确清理到另一次。 The tests pass once you Rebuild the project. 重建项目后,测试将通过。

I had a similar issue too. 我也有类似的问题。 All I needed was to refresh gradle project. 我只需要刷新gradle项目。

  1. Navigate to the Gradle tan in IntelliJ. 导航到IntelliJ中的Gradle tan。
  2. You can click the refresh icon or right click on the project and select 'Refresh Gradle Project'. 您可以单击刷新图标或右键单击项目,然后选择“刷新Gradle项目”。

Re run the test in IntelliJ and the error should go away. 重新在IntelliJ中运行测试,错误应该消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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