Is it possible to use the _data folder for provider tests the same way that stub tests allow

45 Views Asked by At

Is it possible to use the _data folder with json examples when running provider tests (Spring Boot) API in the same way it's possible to do it with stub tests?

I have a quote-api.yaml contract in src/test/contracts/quote-api.yml

I created a folder src/test/contracts/quote-api_data and placed the file happy_path.json in there.

In there I put the following json

{
  "http-request": {
    "method": "POST",
    "path"  : "/quote",
    "body"  : {
      "quoteReference": "VALID_QUOTE_REFERENCE",
      "customers": [
        {
          "customerType": "Z",
          "dateOfBirth" : "2000-01-01"
        },
        {
          "customerType": "Z",
          "dateOfBirth" : "2001-01-01"
        }
      ]
    }
  },
  "http-response": {
    "status" : 200,
    "body" : {
      "fullPrice": 10.0,
      "monthlyPrice": 20.0,
      "quoteStatus": "OK"
    }
  }
}

and my ContractTests looks like


public class ContractTest extends SpecmaticJUnitSupport {
  private static ConfigurableApplicationContext context;

  @BeforeAll
  public static void setUp() {
    System.setProperty("host", "localhost");
    System.setProperty("port", "8080");
    System.setProperty("contractPaths", "src\\test\\contracts\\quotes.yaml");

//    System.setProperty("SPECMATIC_GENERATIVE_TESTS", "true");

    context = SpringApplication.run(HomeownerapiApplication.class);
  }

  @AfterAll
  public static void tearDown() {
    context.close();
  }
}

when I run the ContractTests in Intellij it doesn't appear to pick up the happy_path.json file?

Is it a case that I have to use a .spec file to externalize the examples when running an API(provider) test? If so in the example above, would you know how I represent the customer array as provided in the json above?

Thanks, Mark.

1

There are 1 best solutions below

0
HariKrishnan On

Co-founder, CTO at Specmatic here.

Yes, Specmatic supports externalising test data to JSON files. Here is the documentation. Hope this helps.