Published on

Getting Pact Tests Running on the Provider in Java


In Pact there are various approaches you can use to verify consumer Pacts. The main thing that is consistent across approaches is that you need to wire in an implementation of the Target interface into the test. This interface has different implementations based on what your provider exposes as an endpoint. There are a number of different options like HTTP to test HTTP related providers, AMQP to test queues and the MockMVC version to test purely the REST portion of your contract. In my case I wanted to use Pact to test the provider boundary to ensure that changes to the provider do not result in any downstream processes breaking. I settled on the MockMvcTarget after running into numerous complications using the Spring based approach like having to workout how to get based the app's security in a testing context. I used code similar to the below:

import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.time.Instant;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;

import static org.mockito.Mockito.doReturn;

@VerificationReports({"console", "markdown"})
public class MyRestControllerPactTest {

    /*this is the business service that you need to mock as part of this test
      we are testing the contract i.e. the shape of the response object more than what happens in between */
    private MyService myServiceMock;

    private MyRestController myRestController;

    //this is a Pact object used to run MockMVC tests against the provider endpoint you are testing
    public final MockMvcTarget target = new MockMvcTarget();

    public void setUp() {
        /*we need to initialize the Mockito mocks we defined with the @Mock annotation as we are not and cannot use the Mockito JUnit runner as we need to use the Pact JUnit runner */
        //we use this to output the request and response when the test runs for debugging purposes

        // this is where we mock out the business service used by the REST endpoint we are testing with this Pact
        SomeBusinessObject someBusinessObject = new SomeBusinessObject();

In the above you have to make your @TestTarget public in this case it is the public final MockMvcTarget target = new MockMvcTarget(); line, otherwise you will get the following error when trying to run this test:

java.lang.RuntimeException: How did getFields return a field we couldn't access?

	at org.junit.runners.model.TestClass.getAnnotatedFieldValues(
	at org.junit.runners.ParentRunner$
	at org.junit.runners.ParentRunner$1.schedule(
	at org.junit.runners.ParentRunner.runChildren(
	at org.junit.runners.ParentRunner.access$000(
	at org.junit.runners.ParentRunner$2.evaluate(
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
	at com.intellij.rt.execution.junit.JUnitStarter.main(
Caused by: java.lang.IllegalAccessException: Class org.junit.runners.model.FrameworkField can not access a member of class with modifiers "private final"
	at sun.reflect.Reflection.ensureMemberAccess(
	at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(
	at java.lang.reflect.AccessibleObject.checkAccess(
	at java.lang.reflect.Field.get(
	at org.junit.runners.model.FrameworkField.get(
	at org.junit.runners.model.TestClass.getAnnotatedFieldValues(
	... 16 more