class 的导入和其中方法的使用在 groovy 合同测试脚本中不起作用
Import of class and use of a method in it is not working in groovy contract test script
Mygroovyfile.groovy
-------------------------
org.springframework.cloud.contract.spec.Contract.make {
description('''
given:
blah blah
when:
blah blah
then:
blah blah
''')
request {
method 'GET'
headers {
contentType(applicationJson())
header('Authorization': 'BEARER blahZblah')
}
url value(consumer('blah blah link'), producer('blah blah link'))
}
response {
status 200
body([
blah blah blah "rest body of the stub"
links: [
'self': [
'href': "blah blah link"
]
]
])
testMatchers {
jsonPath("$.links['self']['href']", byCommand('assertThatSelfLinkMatches($it, "blah blah link")'))
}
headers {
contentType(applicationJson())
}
}
}
import companybuild.customlib.GPT;
String GPT(gpt){
return companybuild.customlib.GPT(gpt);
}
and when I do the maven clean install it's giving me an error
[ERROR] Failed to execute goal org.springframework.cloud:spring-cloud-contract-maven-plugin:1.2.1.RELEASE:generateTests (default-generateTests) on project room-pricing-service: Execution default-generateTests of goal org.springframework.cloud:spring-cloud-contract-maven-plugin:1.2.1.RELEASE:generateTests failed: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
[ERROR]service\src\test\resources\contracts\Mygroovyfile.groovy: 108: unable to resolve class companybuild.customlib.GPT
[ERROR] @ line 108, column 2.
[ERROR] import companybuild.customlib.GPT;
[ERROR] ^
[ERROR]
[ERROR] 1 error
[ERROR]
[ERROR] -> [Help 1]
我们目前正在编写实际使用 spring 云合同的合同测试,我们有一个扩展和使用 groovy 文件的基础 java class,我'我在使用 groovy
导入时遇到问题
我想导入我公司构建的自定义 jar,其中有一些 java class 有一些方法 现在的问题是我想在 MyGroovyFile.groovy [=13 中访问=]
提前致谢。
文档中对整个流程进行了非常详尽的描述。请阅读本章 https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_extending_the_dsl . You can also check the samples https://github.com/spring-cloud-samples/spring-cloud-contract-samples/,我们有完全相同的场景。
一个单独的模块https://github.com/spring-cloud-samples/spring-cloud-contract-samples/tree/master/common
在类路径上引用以帮助自动完成
<dependency>
<groupId>com.example</groupId>
<artifactId>beer-common</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
在插件的类路径中引用
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>${spring-cloud-contract.version}</version>
<extensions>true</extensions>
<configuration>
<packageWithBaseClasses>com.example</packageWithBaseClasses>
<baseClassMappings>
<baseClassMapping>
<contractPackageRegex>.*intoxication.*</contractPackageRegex>
<baseClassFQN>com.example.intoxication.BeerIntoxicationBase</baseClassFQN>
</baseClassMapping>
</baseClassMappings>
</configuration>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>beer-common</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>
在 DSL 中
package contracts.beer.rest
import com.example.ConsumerUtils
import com.example.ProducerUtils
import org.springframework.cloud.contract.spec.Contract
Contract.make {
description("""
Represents a successful scenario of getting a beer
```
given:
client is old enough
when:
he applies for a beer
then:
we'll grant him the beer
```
""")
request {
method 'POST'
url '/check'
body(
age: $(ConsumerUtils.oldEnough())
)
headers {
contentType(applicationJson())
}
}
response {
status 200
body("""
{
"status": "${value(ProducerUtils.ok())}"
}
""")
headers {
contentType(applicationJson())
}
}
Mygroovyfile.groovy
-------------------------
org.springframework.cloud.contract.spec.Contract.make {
description('''
given:
blah blah
when:
blah blah
then:
blah blah
''')
request {
method 'GET'
headers {
contentType(applicationJson())
header('Authorization': 'BEARER blahZblah')
}
url value(consumer('blah blah link'), producer('blah blah link'))
}
response {
status 200
body([
blah blah blah "rest body of the stub"
links: [
'self': [
'href': "blah blah link"
]
]
])
testMatchers {
jsonPath("$.links['self']['href']", byCommand('assertThatSelfLinkMatches($it, "blah blah link")'))
}
headers {
contentType(applicationJson())
}
}
}
import companybuild.customlib.GPT;
String GPT(gpt){
return companybuild.customlib.GPT(gpt);
}
and when I do the maven clean install it's giving me an error
[ERROR] Failed to execute goal org.springframework.cloud:spring-cloud-contract-maven-plugin:1.2.1.RELEASE:generateTests (default-generateTests) on project room-pricing-service: Execution default-generateTests of goal org.springframework.cloud:spring-cloud-contract-maven-plugin:1.2.1.RELEASE:generateTests failed: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
[ERROR]service\src\test\resources\contracts\Mygroovyfile.groovy: 108: unable to resolve class companybuild.customlib.GPT
[ERROR] @ line 108, column 2.
[ERROR] import companybuild.customlib.GPT;
[ERROR] ^
[ERROR]
[ERROR] 1 error
[ERROR]
[ERROR] -> [Help 1]
我们目前正在编写实际使用 spring 云合同的合同测试,我们有一个扩展和使用 groovy 文件的基础 java class,我'我在使用 groovy
导入时遇到问题我想导入我公司构建的自定义 jar,其中有一些 java class 有一些方法 现在的问题是我想在 MyGroovyFile.groovy [=13 中访问=]
提前致谢。
文档中对整个流程进行了非常详尽的描述。请阅读本章 https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_extending_the_dsl . You can also check the samples https://github.com/spring-cloud-samples/spring-cloud-contract-samples/,我们有完全相同的场景。
一个单独的模块https://github.com/spring-cloud-samples/spring-cloud-contract-samples/tree/master/common
在类路径上引用以帮助自动完成
<dependency>
<groupId>com.example</groupId>
<artifactId>beer-common</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
在插件的类路径中引用
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>${spring-cloud-contract.version}</version>
<extensions>true</extensions>
<configuration>
<packageWithBaseClasses>com.example</packageWithBaseClasses>
<baseClassMappings>
<baseClassMapping>
<contractPackageRegex>.*intoxication.*</contractPackageRegex>
<baseClassFQN>com.example.intoxication.BeerIntoxicationBase</baseClassFQN>
</baseClassMapping>
</baseClassMappings>
</configuration>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>beer-common</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>
在 DSL 中
package contracts.beer.rest
import com.example.ConsumerUtils
import com.example.ProducerUtils
import org.springframework.cloud.contract.spec.Contract
Contract.make {
description("""
Represents a successful scenario of getting a beer
```
given:
client is old enough
when:
he applies for a beer
then:
we'll grant him the beer
```
""")
request {
method 'POST'
url '/check'
body(
age: $(ConsumerUtils.oldEnough())
)
headers {
contentType(applicationJson())
}
}
response {
status 200
body("""
{
"status": "${value(ProducerUtils.ok())}"
}
""")
headers {
contentType(applicationJson())
}
}