Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

JUnit Test Rule

Wade Pearce edited this page Jan 15, 2015 · 1 revision

The library comes equipped with a JUnit Test Rule.

While this rule can be run as a simple @Rule annotation, it is recommended it is run as a @ClassRule simply due to potential download times for remote resources.

Example Usage

public class MySpecificationTest {
    @Rule
    public MockServerRule server = new MockServerRule(5000, this);
    @ClassRule
    public final static RamlSpecificationsRule SPECIFICATIONS = new RamlSpecificationsRule()
                .withSpecification(
                    new ClassPathSpecification("my-service", "apispecs/apispecs.yml")
                );

    @Test
    public void shouldInteractWithServiceCorrectly() {
        MockServiceClient client = new MockServiceClient("localhost", 5000);

        client.when(
            request()
                .withPath("/hello/world")
                .withMethod("GET")
                .withHeaders(
                    new Header(HttpHeaders.ACCEPT, "text/plain"),
                    new Header(HttpHeaders.AUTHORIZATION, "Basic TOKEN1234")
                )
        ).respond(
            response()
                .withStatusCode(200)
                .withHeaders(
                    new Header(HttpHeaders.CONTENT_TYPE, "text/plain"
                )
                .withBody("Hello World!")
        );

        Client testClient = Client.create(new DefaultClientConfig());

        ClientResponse response = testClient.resource("http://localhost:5000/hello/world").get(ClientResponse.class);

        try {
            assertThat(response.getStatus(), is(200));
            assertThat(response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE), is("text/plain"));
            assertThat(response.getEntity(String.class), is("Hello world!"));
        } finally {
            response.close();
        }
    }

    RamlSpecification.Result specificationResult = SPECIFICATIONS.get("my-service").obeyedBy(client);

    assertTrue(specificationResult.getFormattedErrorMessage(), specificationResult.isValid());
}
  • Home
  • [RAML Specifications](RAML Specifications)
    • [Class Path Specification](Class Path Specifications)
    • [File Path Specification](File Path Specifications)
    • [Remote Specification](Remote Path Specifications)
      • Handlers
        • [Zip Archive](Zip Archive Handler)
        • [RAML File](RAML File Handler)
        • [Custom](Custom Resource Handler)
    • [Custom Specification](Custom Specification)
  • [JUnit Test Rule](JUnit Test Rule)
Clone this wiki locally