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

Class Path Specifications

Wade Pearce edited this page Jan 15, 2015 · 2 revisions

If your RAML specification exists within your own classpath (either in your own resources or a provided dependency resource), you can choose to load that specification directly for testing purposes using the ClassPathSpecification implementation.

Example Usage

RamlSpecification specification = new ClassPathSpecification("my-service", "apispecs/apispecs.yml");

Example Unit Test

public class MySpecificationTest {
    @Rule
    public MockServerRule server = new MockServerRule(5000, this);

    private final static RamlSpecification SPECIFICATION = new ClassPathSpecification("my-service", "apispecs/apispecs.yml");

    @BeforeClass
    public static void setUpClass() {
        SPECIFICATION.initialize();
    }

    @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 = SPECIFICATION.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