我想在 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 - 春季云合约插件配置:
<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)导入静态常量时,它可以工作。
有什么建议可以解决这个问题或如何在更多常规合同文件中共享变量?谢谢!