我正在使用协议 gradle 插件以及消费者/提供者依赖项为队列中的消息创建协议测试。
但是在运行时./gradlew clean pactVerify
出现错误:
Verification Failed - Could not load class mypackage.MyClass :
java.lang.UnsupportedClassVersionError: mypackage/MyClass has been compiled by a more
recent version of the Java Runtime (class file version 59.0), this version of the Java
Runtime only recognizes class file versions up to 55.0
即使我使用 Java 15 进行编译,并且https://github.com/pact-foundation/pact-jvm声明 4.2.x 与 Java 15 兼容。
消费者守则:
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "Provider", providerType = ProviderType.ASYNCH)
class PactTest {
@Pact(consumer = "consumer")
MessagePact createPact(MessagePactBuilder builder) {
PactDslJsonBody body = new PactDslJsonBody()
.stringValue("testParam1", "value1")
.stringValue("testParam2", "value2");
return builder.given("ProviderOk")
.expectsToReceive("a valid message")
.withContent(body)
.toPact();
}
@Test
@PactTestFor(pactMethod = "createPact")
void test(List<Message> messages) {
assertThat(new String(messages.get(0).contentsAsBytes()), is("{\"testParam1\":\"value1\",\"testParam2\":\"value2\"}"));
}
}
消费者配置:
plugins {
id("au.com.dius.pact") version "4.2.9"
}
dependencies {
testImplementation("au.com.dius.pact", "consumer", "4.2.9")
testImplementation("au.com.dius.pact.consumer", "junit5", "4.2.9")
}
pact {
publish {
pactDirectory = "build/pacts"
consumerVersion = version
}
}
提供者代码:
class PactTest {
@PactVerifyProvider("a valid message")
String verifyMessage() {
return "{\"testParam1\":\"value1\",\"testParam2\":\"value2\"}";
}
}
提供者配置:
plugins {
id("au.com.dius.pact") version "4.2.9"
}
dependencies {
testImplementation("au.com.dius.pact", "provider", "4.2.9")
}
pact {
serviceProviders {
create("Provider") {
verificationType = ANNOTATED_METHOD
packagesToScan = listOf("...mypackage...")
hasPactWith("ICS") {
pactSource = file("config/consumer-Provider.json")
}
}
}
}
我从中获得的提供程序示例代码: https ://github.com/pact-foundation/pact-jvm/tree/master/provider/gradle#verifying-a-message-provider