0

Hope someone might be able to help me, been beating my head against the wall for a while and can't seem to break through. I have tried following multiple examples and doing endless searching, just can't seem to find out what I am missing. There are a lot of examples to follow but most are spring applications and I know nothing about spring so I don't know if I am trying to mix spring with non-spring code...

This is NOT a spring application nor do I want it to be. I am trying to get this going as just a simple java producer verification. Pretty much as simple as it can be...

I have a good contract and I am able to get a successful verification of the contract using JS but want to use JAVA.

I have way too many imports in my code but don't know at this point which ones are not important and or are duplications.

I have a few compile errors but can't seem to find out how to resolve. I noted the compile errors in the code. I think I am close but just don't know at this point.

Is the pactTestTemplate method the one that actually does the contract verification? So that is the method I would call from my unit test? Really confused at this point as to all the magic that is going on in the background...

If you made it this far, thanks...

package testWorkContract;

import au.com.dius.pact.provider.junit.PactRunner;
import au.com.dius.pact.provider.junit.Provider;
import au.com.dius.pact.provider.junit5.*;
import au.com.dius.pact.provider.junit5.PactVerificationContext;
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
import org.junit.jupiter.api.extension.*;
import org.junit.runner.RunWith;
import au.com.dius.pact.provider.junit.loader.PactFolder;
import au.com.dius.pact.provider.junit.loader.PactUrl;
import au.com.dius.pact.provider.junit.target.HttpTarget;
import au.com.dius.pact.provider.junit.target.Target;
import au.com.dius.pact.provider.junit.target.TestTarget;

import org.apache.http.HttpRequest;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.runner.RunWith;
import au.com.dius.pact.provider.junit.*;


@PactFolder("/temp/pacts")
@RunWith(PactRunner.class) // Say JUnit to run tests with custom Runner
@Provider("work") // Set up name of tested provider
public class PactProvider {

    @TestTarget // Annotation denotes Target that will be used for tests
    public final HttpsTestTarget target = new HttpsTestTarget("127.0.0.1", 443);

    @BeforeClass // Method will be run once: before whole contract test suite
    public static void setUpService() {
    }

    @Before // Method will be run before each test of interaction
    public void before() {
    }

    // JUST A QUICK TEST METHOD TO MAKE SURE MY UNIT TEST CAN WORK...
    public int testService() {
        return 0;
    }
    
    @TestTemplate   // THIS IS AN ERROR, CAN'T FIND @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void pactTestTemplate(PactVerificationContext context, HttpRequest request) {
        context.verifyInteraction();
    }

}

Here are my dependencies, first time setting up maven also so there might be problems there:

    <properties>
        <json-schema-validator.version>3.3.0</json-schema-validator.version>
        <junit.jupiter.version>5.5.2</junit.jupiter.version>
        <pact.version>4.0.10</pact.version>
        <maven.surefire.version>3.0.0-M5</maven.surefire.version>
    </properties>
  
  <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>au.com.dius</groupId>
            <artifactId>pact-jvm-consumer-junit5</artifactId>
            <version>${pact.version}</version>
        </dependency>
        <dependency>
            <groupId>au.com.dius</groupId>
            <artifactId>pact-jvm-provider-junit</artifactId>
            <version>${pact.version}</version>
        </dependency>
        <dependency>
            <groupId>au.com.dius</groupId>
            <artifactId>pact-jvm-provider-junit5</artifactId>
            <version>${pact.version}</version>
        </dependency>
  </dependencies>
Btah
  • 1
  • 2

1 Answers1

0

I was able to get past my problems. I was thinking about this incorrectly. The code I was trying to write SHOULD HAVE BEEN THE ACTUAL JUNIT test. I was trying to write a junit test that then called the class I was trying to put together.

This is the junit test I ended up with for starters

package testWorkContract;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testng.annotations.Test;

//import au.com.dius.pact.core.model.annotations.PactFolder;
import au.com.dius.pact.provider.junit.loader.PactFolder;
import au.com.dius.pact.provider.junit.Provider;
import au.com.dius.pact.provider.junit.loader.PactSource;
import au.com.dius.pact.provider.junit5.HttpsTestTarget;
import au.com.dius.pact.provider.junit5.PactVerificationContext;
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
import junit.framework.Assert;

@Provider("work")
@PactFolder("/temp/pacts")

public class PactProviderTest {

      @BeforeEach
      void before(PactVerificationContext context) {
//      context.setTarget(HttpTestTarget.fromUrl(new URL(myProviderUrl)));
        // or something like
         context.setTarget(new HttpsTestTarget("localhost", 443, "/", true));
      }
      
      @Test
        @TestTemplate
        @ExtendWith(PactVerificationInvocationContextProvider.class)
        void pactVerificationTestTemplate(PactVerificationContext context) {
          context.verifyInteraction();
        }

}

and this is the pom file

    <properties>
        <json-schema-validator.version>3.3.0</json-schema-validator.version>
        <junit.jupiter.version>5.5.2</junit.jupiter.version>
        <pact.version>4.0.10</pact.version>
        <maven.surefire.version>3.0.0-M5</maven.surefire.version>
    </properties>
  
  <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.5.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>au.com.dius</groupId>
            <artifactId>pact-jvm-consumer-junit5</artifactId>
            <version>${pact.version}</version>
        </dependency>
        <dependency>
            <groupId>au.com.dius</groupId>
            <artifactId>pact-jvm-provider-junit</artifactId>
            <version>${pact.version}</version>
        </dependency>
        <dependency>
            <groupId>au.com.dius</groupId>
            <artifactId>pact-jvm-provider-junit5</artifactId>
            <version>${pact.version}</version>
        </dependency>
  </dependencies>
        
        <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
                
            <plugin>
                <groupId>au.com.dius.pact.provider</groupId>
                <artifactId>maven</artifactId>
                <version>4.1.11</version>
                <configuration>
                  <serviceProviders>
                    <!-- You can define as many as you need, but each must have a unique name -->
                    <serviceProvider>
                      <name>work</name>
                      <!-- All the provider properties are optional, and have sensible defaults (shown below) -->
                      <protocol>https</protocol>
                      <host>localhost</host>
                      <port>443</port>
                      <path>/</path>
                      <insecure>true</insecure>
                      <pactFileDirectories>
                      
                                  <pactFileDirectory>/temp/pacts</pactFileDirectory>
                      </pactFileDirectories>
                      
                    </serviceProvider>
                  </serviceProviders>
                </configuration>
            </plugin>
            
        </plugins>
        </build>
Btah
  • 1
  • 2