Spring 云合同:是否可以在 groovy 合同文件中使用 Java 中的常量?
Spring cloud contract: is possible to use constant from Java in grrovy Contract file?
我想在 groovy 合同中分享 Java classes 中的常量。
试验基地class:
@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 云合约插件配置:
<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 全新安装并获取
[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,它起作用了。
关于如何解决这个问题或如何在更多 groovy 合同文件中共享变量的任何建议?
谢谢!
是的,请阅读文档的这一部分 - https://docs.spring.io/spring-cloud-contract/docs/current/reference/html/advanced.html#customization-customization . Here you can see the code with shared stuff - https://github.com/spring-cloud-samples/spring-cloud-contract-samples/tree/main/common . Here you can see how it's added on the producer side https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/main/producer/pom.xml#L106-L111 and here on the consumer https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/main/consumer/pom.xml#L63-L68
我想在 groovy 合同中分享 Java classes 中的常量。
试验基地class:
@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 云合约插件配置:
<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 全新安装并获取
[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,它起作用了。
关于如何解决这个问题或如何在更多 groovy 合同文件中共享变量的任何建议? 谢谢!
是的,请阅读文档的这一部分 - https://docs.spring.io/spring-cloud-contract/docs/current/reference/html/advanced.html#customization-customization . Here you can see the code with shared stuff - https://github.com/spring-cloud-samples/spring-cloud-contract-samples/tree/main/common . Here you can see how it's added on the producer side https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/main/producer/pom.xml#L106-L111 and here on the consumer https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/main/consumer/pom.xml#L63-L68