我想在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)导入静态常量时,它可以工作。
有关如何查看此内容或如何在更多常规合同文件中共享变量的任何建议? 谢谢!
答案 0 :(得分:1)
是的,请阅读文档的这一部分 - https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html#_extending_the_dsl。在这里,您可以看到包含共享内容的代码 - https://github.com/spring-cloud-samples/spring-cloud-contract-samples/tree/master/common。在这里,您可以看到它是如何在生产者方https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/producer/pom.xml#L106-L111和消费者https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/consumer/pom.xml#L63-L68
上添加的