Spring云合约:可以在grrovy Contract文件中使用Java中的常量吗?

时间:2017-05-19 14:18:46

标签: spring-cloud-contract

我想在groovy Contracts中分享来自Java类的常量。

测试基类:

    @SpringBootTest(classes = TestBase.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestBase {

    public static final String NAME = "just constant";

    @Before
    public void setup() {
        RestAssured.baseURI = "https://rest-service.com";
        RestAssured.port = 443;
    }
}

合同文件:

package contracts.test_contract

import org.springframework.cloud.contract.spec.Contract
import static test.TestBase.NAME;

Contract.make {
    request {
        method 'GET'
        url ''
        body (
                """${value(client(NAME), server(NAME))}"""
        )
    }
    response {
        status 200
    }
}

pom.xml - spring cloud contract plugin config:

<plugin>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-contract-maven-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <testMode>EXPLICIT</testMode>
                    <baseClassForTests>test.TestBase</baseClassForTests>
                </configuration>
            </plugin>

运行mvn clean install并获取

[ERROR] Exception occurred while trying to evaluate the contract at path [C:\dev\_idea_workspace\test_1\src\test\resources\contracts\test_contract\c1.groovy]
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\dev\_idea_workspace\test_1\src\test\resources\contracts\test_contract\c1.groovy: 7: unable to resolve class test.TestBase
 @ line 7, column 1.
   import static test.TestBase.NAME;
   ^

1 error

但是当我尝试从另一个文件(例如Long.MAX_VALUE)导入静态常量时,它可以工作。

有关如何查看此内容或如何在更多常规合同文件中共享变量的任何建议? 谢谢!