Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Grails测试中使用Groovy特性失败_Grails_Groovy_Spock - Fatal编程技术网

在Grails测试中使用Groovy特性失败

在Grails测试中使用Groovy特性失败,grails,groovy,spock,Grails,Groovy,Spock,如何在Grails Spock测试中实现Groovy的新特性?每次我尝试,我都会得到一个像这样的stacktrace。Groovy轨迹是否存在一些我可能不知道的限制 JDK版本: java version "1.7.0_65" OpenJDK Runtime Environment (IcedTea 2.5.3) (7u71-2.5.3-0ubuntu0.14.04.1) OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode) Groovy

如何在Grails Spock测试中实现Groovy的新特性?每次我尝试,我都会得到一个像这样的stacktrace。Groovy轨迹是否存在一些我可能不知道的限制

JDK版本:

java version "1.7.0_65"
OpenJDK Runtime Environment (IcedTea 2.5.3) (7u71-2.5.3-0ubuntu0.14.04.1)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)
Groovy Verison:

Groovy Version: 2.3.6 JVM: 1.7.0_65 Vendor: Oracle Corporation OS: Linux
Grails版本:

Grails version: 2.4.3
简化代码:

import grails.test.mixin.Mock
@Mock([AnalyticFilters])
trait ControllerTestBase {
public void setupCommonStuff(boolean setupIdCall = false) {
params.devId = TEST_DEV_ID
    // mocking version filter
    params.version = "v${TEST_VERSION}"

    defineBeans{
       CacheService(cacheServiceMock: "createMock")
    }

    CommonParams.parseParams(params)

    cacheMock = applicationContext.getBean("cacheServiceMock")

    if(setupStoreIdCall) {
        cacheMock.demandExplicit.makeCompositeKey(0..20) { List<String> list ->
            def (String uuid, String orgUuid) = list
            return "foobar"
        }
    }

}
}

@TestFor(AuditController)
public class AuditControllerSpecs extends Specification implements ControllerTestBase {

private def auditServiceFactory

public String testAuditData = "{audit:'whatever'}"

public void setup() {
    setupCommonParams()

    defineBeans {
        auditServiceFactory(GrailsMock, AuditService)
        auditService(auditServiceFactory: "createMock")
    }
    auditServiceFactory = applicationContext.getBean("auditServiceFactory")
    auditServiceFactory.demand.writeEventToMongo { BasicDBObject data -> }
    controller.auditService = applicationContext.getBean('auditService', AuditService)
}

def "calling productAudit should return with 200 and OK"() {

    given:
    request.JSON = JSON.parse(testAuditData)
    request.method = 'POST'
    when:
    withFilters(action: "productAudit") {
        controller.productAudit()
    }
    then:

    def res = JSON.parse(response.text)

    expect:
    response.status == 200
    res.message == "OK"
    100 == 2
}

在我看来,使用traits的脚本字段有一个限制。如果使用trait的实现中的字段数量达到一定的大小(我的案例中有34个方法),则会引发此异常。我现在遇到了和你们一样的问题,我正在努力解决它

我可以通过以下方式绕过Groovy:

-Djava.util.Arrays.useLegacyMergeSort=true
如本文所述:


我认为这与他们的GETTER\u FIRST\u比较器有关,它不返回0。它只返回1和-1,我认为这就是它提到违反合同的原因

我们的项目中也出现了同样的例外情况,大量使用了以下特征:

java.lang.IllegalArgumentException:比较方法违反了它的一般约定

我的同事发现,只要一个特征超过10个字段,就会发生这种情况。我们不知道这种行为的起源

作为一种解决方法,我们利用性状遗传:

trait Foo extends MoreFoo {
    //this has 10 fields
}

trait MoreFoo{
    //this has some more fields but not more than 10
}

值得注意的是,我们在Android上使用Groovy,因此按照建议调整JVM选项不是一个选项。

取决于它的使用方式。你能补充一下你在做什么吗?你能告诉我你使用的是哪个JVM版本吗?trait中有10个字段出现问题,删除了一个字段,所有字段都恢复正常,有趣的是,在我重新启动计算机之前,一切都是正常的,只是遇到了完全相同的模棱两可的编译器错误,带有10+个字段的特征-将数字降低到@TomBunting我很高兴您发现了这个问题,并且没有在这个问题上浪费更多的时间。这确实是一个奇怪的错误,在处理特征时很难定位。
trait Foo extends MoreFoo {
    //this has 10 fields
}

trait MoreFoo{
    //this has some more fields but not more than 10
}