Hello All, I am trying to write tests for Grpc api...
# protobufs
p
Hello All, I am trying to write tests for Grpc api's (Java, Maven) using pact protobuf plugin. I installed plugin and trying to write tests using the example and I am getting this error while using Mockserver annotation.
java.lang.NoSuchFieldError: TRANSPORT
any help please?
🤔 1
☺️ 1
Copy code
import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.PactBuilder;
import au.com.dius.pact.consumer.junit.MockServerConfig;
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
import au.com.dius.pact.consumer.junit5.PactTestFor;
import au.com.dius.pact.consumer.junit5.ProviderType;
import au.com.dius.pact.consumer.model.MockServerImplementation;
import au.com.dius.pact.core.model.PactSpecVersion;
import au.com.dius.pact.core.model.V4Interaction;
import au.com.dius.pact.core.model.V4Pact;
import au.com.dius.pact.core.model.annotations.Pact;
import com.atoss.template.servicetemplate.adapter.handler.grpc.employee.v1.EmployeeServiceGrpc;
import com.atoss.template.servicetemplate.adapter.handler.grpc.employee.v1.Message;
import com.atoss.template.servicetemplate.adapter.handler.grpc.employee.v1.MessageRequest;
import com.google.protobuf.InvalidProtocolBufferException;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.List;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static <http://org.hamcrest.Matchers.is|org.hamcrest.Matchers.is>;

@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "adapter-handler-grpc", providerType = ProviderType.SYNCH_MESSAGE, pactVersion = PactSpecVersion.V4)
public class PactConsumerTest {

    @Pact(consumer = "adapter-client-grpc")
    V4Pact testMsg(PactBuilder builder) {
        return builder
                // Tell Pact we need the Protobuf plugin
                .usingPlugin("protobuf", "0.1.8")
                // We will use a V4 synchronous message interaction for the test
                .expectsToReceive("message", "core/interaction/synchronous-message")
                // We need to pass all the details for the interaction over to the plugin
                .with(Map.of(
                        // Configure the proto file, the content type and the service we expect to invoke
                        "pact:proto", builder.filePath("../adapter-handler-grpc-proto/src/main/resources/proto/EmployeeService.proto"),
                        "pact:content-type", "application/grpc",
                        "pact:proto-service", "EmployeeService/getEmployee",

                        // Details on the request message (ShapeMessage) we will send
                        "request", Map.of("id", 1),

                        // Details on the response message we expect to get back (AreaResponse)
                        "response", List.of(
                                Map.of(
                                        "msg", "Hello Grpc"
                                )
                        )
                ))
                .toPact();
    }

    @Test
    @PactTestFor(pactMethod = "testMsg")
    @MockServerConfig(implementation = MockServerImplementation.Plugin, registryEntry = "protobuf/transport/grpc")
    void readMessage(MockServer mockServer, V4Interaction.SynchronousMessages interaction) throws InvalidProtocolBufferException {
        ManagedChannel channel = ManagedChannelBuilder.forTarget("[::1]:" + mockServer.getPort())
                .usePlaintext()
                .build();
        EmployeeServiceGrpc.EmployeeServiceBlockingStub clientStub = EmployeeServiceGrpc
                .newBlockingStub(channel);

        Message response = clientStub.getMessage(MessageRequest.newBuilder().setId(1).build());
        System.out.println("after response");
        assertThat(response.getMsg(), is("{\"msg\":\"Hello Grpc\"}"));
    }

}
This is the provider consumer test I that is causing issue
u
Can you provide debug logs?
p
Hello sure,
[INFO] Running com.atoss.template.servicetemplate.adapter.client.grpc.employee.pact.PactConsumerTest
091546.174 [main] DEBUG au.com.dius.pact.consumer.junit5.PactConsumerTestExt - Found @PactTestFor annotation on test method void com.atoss.template.servicetemplate.adapter.client.grpc.employee.pact.PactConsumerTest.readMessage() throws com.google.protobuf.InvalidProtocolBufferException 091546.175 [main] DEBUG au.com.dius.pact.consumer.junit5.PactConsumerTestExt - Found @PactTestFor annotation on test class com.atoss.template.servicetemplate.adapter.client.grpc.employee.pact.PactConsumerTest 091546.324 [main] DEBUG au.com.dius.pact.consumer.junit5.PactConsumerTestExt - providerInfo = ProviderInfo(providerName=adapter-handler-grpc, hostInterface=, port=, pactVersion=V4, providerType=SYNCH_MESSAGE, https=false, mockServerImplementation=Default, keyStorePath=, keyStoreAlias=, keyStorePassword=, privateKeyPassword=) 091546.333 [main] DEBUG au.com.dius.pact.consumer.junit5.PactConsumerTestExt - Looking for @Pact method named 'testMsg' for provider 'adapter-handler-grpc' 091546.345 [main] DEBUG au.com.dius.pact.consumer.junit5.PactConsumerTestExt - Invoking method 'testMsg' to get Pact for the test 'readMessage' 091546.911 [Thread-1] WARN au.com.dius.pact.core.support.Metrics - Please note: we are tracking events anonymously to gather important usage statistics like JVM version and operating system. To disable tracking, set the 'pact_do_not_track' system property or environment variable to 'true'. 091546.949 [main] INFO au.com.dius.pact.consumer.junit5.PactConsumerTestExt - Writing pacts out to default directory [ERROR] Tests run: 2, Failures: 1, Errors: 1, Skipped: 0, Time elapsed: 0.801 s <<< FAILURE! - in com.atoss.template.servicetemplate.adapter.client.grpc.employee.pact.PactConsumerTest [ERROR] readMessage Time elapsed: 0.772 s <<< ERROR! java.lang.NoSuchFieldError: TRANSPORT [ERROR] com.atoss.template.servicetemplate.adapter.client.grpc.employee.pact.PactConsumerTest Time elapsed: 0.799 s <<< FAILURE! java.lang.AssertionError: The following methods annotated with @Pact were not executed during the test: PactConsumerTest.testMsg If these are currently a work in progress, add a @Disabled annotation to the method [INFO] [INFO] Results: [INFO] [ERROR] Failures: [ERROR] PactConsumerTest The following methods annotated with @Pact were not executed during the test: PactConsumerTest.testMsg If these are currently a work in progress, add a @Disabled annotation to the method [ERROR] Errors: [ERROR] PactConsumerTest.readMessage » NoSuchField TRANSPORT [INFO] [ERROR] Tests run: 16, Failures: 1, Errors: 1, Skipped: 0 [INFO]
u
Can you check that you are using the same version for all the Pact-JVM libraries
p
I am using below dependencies
Copy code
<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java-util</artifactId>
    <version>3.21.4</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.pact.plugin.driver</groupId>
    <artifactId>core</artifactId>
    <version>0.0.7</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>au.com.dius.pact.consumer</groupId>
     <artifactId>junit5</artifactId>
      <version>4.4.0-beta.3</version>
 </dependency>
u
Remove this one: io.pact.plugin.driver
p
and protobuf plugin 0.1.10 , do I need any other dependencies? Is there an example with maven java?
If I do not use io.pact.plugin.driver, I get the error
[main] INFO <http://au.com|au.com>.dius.pact.consumer.junit5.PactConsumerTestExt - Writing pacts out to default directory
[ERROR] Tests run: 2, Failures: 1, Errors: 1, Skipped: 0, Time elapsed: 0.564 s <<< FAILURE! - in com.atoss.template.servicetemplate.adapter.client.grpc.employee.pact.PactConsumerTest [ERROR] readMessage{MockServer, SynchronousMessages} Time elapsed: 0.541 s <<< ERROR! java.lang.NoSuchMethodError: au.com.dius.pact.core.support.Utils.lookupEnvironmentValue$default(Lau/com/dius/pact/core/support/Utils;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String; at com.atoss.template.servicetemplate.adapter.client.grpc.employee.pact.PactConsumerTest.MessageTest(PactConsumerTest.java:36) [ERROR] com.atoss.template.servicetemplate.adapter.client.grpc.employee.pact.PactConsumerTest Time elapsed: 0.562 s <<< FAILURE! java.lang.AssertionError: The following methods annotated with @Pact were not executed during the test: PactConsumerTest.MessageTest If these are currently a work in progress, add a @Disabled annotation to the method 093524.510 [Thread-1] DEBUG io.pact.plugins.jvm.core.DefaultPluginManager - SHUTDOWN - shutting down all plugins [INFO] [INFO] Results: [INFO] [ERROR] Failures: [ERROR] PactConsumerTest The following methods annotated with @Pact were not executed during the test: PactConsumerTest.MessageTest If these are currently a work in progress, add a @Disabled annotation to the method [ERROR] Errors: [ERROR] PactConsumerTest.MessageTest:36 » NoSuchMethod au.com.dius.pact.core.support.U... [INFO] [ERROR] Tests run: 16, Failures: 1, Errors: 1, Skipped: 0 [INFO]
u
Then use the latest version, which is 0.1.3
There is something wrong with your dependencies, you shouldn't need to add that library, it should be automatically added from the Pact-JVM ones
p
0.1.3 for which dependency? I only have one Pact dependency consumer. I am using latest from maven
Copy code
<dependency>
    <groupId>au.com.dius.pact.consumer</groupId>
    <artifactId>junit5</artifactId>
    <version>4.4.0-beta.3</version>
</dependency>
is this not the latest one ?
u
For
io.pact.plugin.driver
p
ok, I will try with this
Thank you ,It was solved after upgrading dependency, I have installed plugin as per readme in $Home/.pact/plugins/protobuf-0.1.10 but I am getting PluginNotFoundException , are there any additional steps required?