1

这个错误的原因是什么,我该如何解决?

乍一看,这似乎是 groovy 编译器的一个缺陷。

:compileIntegrationTestGroovystartup failed:
C:\src\my-project\src\integration-test\groovy\my\project\MyServiceISpec.groovy: 31: The method setup should be public as it implements the corresponding method from interface my.project.MyTrait
. At [31:5]  @ line 31, column 5.
       public void setup() {
       ^

1 error

我的 grails 集成测试如下所示:

@Integration
@Rollback
class MyServiceISpec extends Specification implements MyTrait {
    @Autowired
    MyService service

    OtherService otherService = Mock()

    public void setup() {
        myTraithMethod()
        service.otherService = otherService
    }
}

我的性格是这样的:

trait MyTrait {
    public void setup() {
        myTraithMethod()
    }

    private myTraitMethod() {
        ...
    }
}

更新public为特征设置方法添加了关键字。

4

2 回答 2

4

我认为这个问题的根源是 AST,因为 Spock 使用 AST 转换并编译规范。你可以在这里阅读http://docs.groovy-lang.org/next/html/documentation/core-traits.html#_compatibility_with_ast_transformations这个:

特征与 AST 转换不正式兼容。其中一些,例如 @CompileStatic 将应用于 trait 本身(而不是实现类),而另一些将应用于实现类和 trait。绝对不能保证 AST 转换会像在常规类上一样在特征上运行,因此使用它需要您自担风险!

例如,您可以通过重命名setup()特征中的方法traitSetup()并从规范setup()方法中调用它来解决它,如下所示:

@Integration
@Rollback
class MyServiceISpec extends Specification implements MyTrait {
    @Autowired
    MyService service
    OtherService otherService = Mock()

    void setup() {
        service.otherService = otherService
        traitSetup()
    }

    def 'some test here'() {
        ...
    }
}

trait MyTrait {
    void traitSetup() {
        myTraitMethod()
    }

    private myTraitMethod() {
        ...
    }
}
于 2019-06-07T11:29:16.260 回答
0

1/ 不确定,但特征名称是 ResetsDatabase 并且您的测试实现了 MyTrait。可能与特质有些混淆?2/在我看来,如果您的特征表明该方法(此处设置)是私有的,则您不能在已实现的方法上使用公共方法。

于 2019-06-05T17:03:16.307 回答