Class JaxrsTestHelper


  • public class JaxrsTestHelper
    extends Object
    Helper class for testing JAX-RS code.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int UNPROCESSABLE_ENTITY_STATUS
      Status code for (non-standard) 422 Unprocessable Entity responses.
    • Constructor Summary

      Constructors 
      Constructor Description
      JaxrsTestHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertAcceptedResponse​(javax.ws.rs.core.Response response)
      Asserts that response is a 202 Accepted response.
      static void assertBadRequest​(javax.ws.rs.core.Response response)
      Asserts the specified response has a 400 Bad Request status.
      static void assertClientErrorResponseFamily​(javax.ws.rs.core.Response response)
      Asserts that response has a 4xx status code.
      static void assertConflict​(javax.ws.rs.core.Response response)
      Asserts the specified response has a 409 Conflict status.
      static void assertCreatedResponse​(javax.ws.rs.core.Response response)
      Asserts that response is a 201 Created response.
      static void assertCreatedResponseWithLocation​(javax.ws.rs.core.Response response, String expectedLocation)
      Asserts that response is a 201 Created response with the Location header set to expectedLocation.
      static void assertCreatedResponseWithLocation​(javax.ws.rs.core.Response response, String basePath, Object id)
      Asserts that response is a 201 Created response with the Location header set to the value of basePath concatenated with id.
      static void assertCreatedResponseWithLocationEndingWith​(javax.ws.rs.core.Response response, String locationEndingWith)
      Asserts that response is a 201 Created response with the Location header having a location path ending with locationEndingWith.
      static void assertCustomHeaderFirstValue​(javax.ws.rs.core.Response response, String headerName, Object expectedValue)
      Asserts that the response has a custom header named headerName and whose first value is expectedValue.
      static <T> void assertEqualResponseEntity​(javax.ws.rs.core.Response response, T expectedEntity)
      Asserts that the response contains an entity using Response.getEntity() that is equal to the expectedEntity using logical equality, e.g.
      static void assertForbiddenResponse​(javax.ws.rs.core.Response response)
      Asserts that response is a 403 Forbidden response.
      static void assertInternalServerErrorResponse​(javax.ws.rs.core.Response response)
      Asserts that response is a 500 Internal Server Error response.
      static void assertJsonResponse​(javax.ws.rs.core.Response response)
      Asserts the actual response type of response is application/json.
      static Map<String,​Object> assertMapResponseEntity​(javax.ws.rs.core.Response response)
      Asserts that the response contains an entity using Response.getEntity() that is a Map and specifically one whose keys are Strings and values are Objects.
      static void assertNoContentResponse​(javax.ws.rs.core.Response response)
      Asserts that response is a 204 No Content response.
      static <T> T assertNonNullResponseEntity​(javax.ws.rs.core.Response response, Class<T> expectedClass)
      Asserts that the response contains an entity using Response.getEntity() whose class is expectedClass.
      static void assertNotFoundResponse​(javax.ws.rs.core.Response response)
      Asserts that response is a 404 Not Found response.
      static void assertOkResponse​(javax.ws.rs.core.Response response)
      Asserts that response is a 200 OK response.
      static void assertPlainTextResponse​(javax.ws.rs.core.Response response)
      Asserts the actual response type of response is text/plain.
      static <T> T assertResponseEntity​(javax.ws.rs.core.Response response, Class<T> expectedClass, T expectedEntity)
      Asserts that the response contains an entity using Response.getEntity() whose class is expectedClass and which is the same instance as the expectedEntity.
      static <T> void assertResponseEntity​(javax.ws.rs.core.Response response, T expectedEntity)
      Asserts that the response contains an entity using Response.getEntity() that is the same instance as the expectedEntity.
      static void assertResponseFamily​(javax.ws.rs.core.Response response, javax.ws.rs.core.Response.Status.Family expectedFamily)
      Asserts that response is in the expected Response.Status.Family.
      static void assertResponseStatusCode​(javax.ws.rs.core.Response response, int expectedStatusCode)
      Asserts the actual response status code is expectedStatusCode.
      static void assertResponseStatusCode​(javax.ws.rs.core.Response response, javax.ws.rs.core.Response.Status expectedStatus)
      Asserts the actual response expectedStatus code is expectedStatus.
      static void assertResponseType​(javax.ws.rs.core.Response response, String expectedContentType)
      Asserts the actual response type of response is expectedContentType.
      static void assertServerErrorResponseFamily​(javax.ws.rs.core.Response response)
      Asserts that response has a 5xx status code.
      static void assertSuccessfulResponseFamily​(javax.ws.rs.core.Response response)
      Asserts that response has a 2xx status code.
      static void assertUnauthorizedResponse​(javax.ws.rs.core.Response response)
      Asserts that response is a 401 Unauthorized response.
      static void assertUnprocessableEntity​(javax.ws.rs.core.Response response)
      Asserts the specified response has a 422 Unprocessable Entity status.
      static void assertXmlResponse​(javax.ws.rs.core.Response response)
      Asserts the actual response type of response is application/xml.
      static javax.ws.rs.core.UriInfo createMockUriInfoWithRequestUriBuilder​(String basePath)
      Creates a Mockito mock {Kink javax.ws.rs.core.UriInfo} object with the specified base path, and which returns a UriBuilder when the UriInfo.getRequestUriBuilder() method is called.
    • Field Detail

      • UNPROCESSABLE_ENTITY_STATUS

        public static final int UNPROCESSABLE_ENTITY_STATUS
        Status code for (non-standard) 422 Unprocessable Entity responses.

        Because it is defined as a WebDAV extension, the JAX-RS Response.Status does not include it as an enum constant, which is...unfortunate.

        See Also:
        422 Unprocessable Entity, Constant Field Values
    • Constructor Detail

      • JaxrsTestHelper

        public JaxrsTestHelper()
    • Method Detail

      • createMockUriInfoWithRequestUriBuilder

        public static javax.ws.rs.core.UriInfo createMockUriInfoWithRequestUriBuilder​(String basePath)
        Creates a Mockito mock {Kink javax.ws.rs.core.UriInfo} object with the specified base path, and which returns a UriBuilder when the UriInfo.getRequestUriBuilder() method is called.

        You are obviously free to record additional expectations as well.

        Parameters:
        basePath - the base path
        Returns:
        a mock UriInfo
      • assertJsonResponse

        public static void assertJsonResponse​(javax.ws.rs.core.Response response)
        Asserts the actual response type of response is application/json.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response type is not the expected value
      • assertXmlResponse

        public static void assertXmlResponse​(javax.ws.rs.core.Response response)
        Asserts the actual response type of response is application/xml.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response type is not the expected value
      • assertPlainTextResponse

        public static void assertPlainTextResponse​(javax.ws.rs.core.Response response)
        Asserts the actual response type of response is text/plain.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response type is not the expected value
      • assertResponseType

        public static void assertResponseType​(javax.ws.rs.core.Response response,
                                              String expectedContentType)
        Asserts the actual response type of response is expectedContentType.
        Parameters:
        response - the response to check
        expectedContentType - expected content type
        Throws:
        AssertionError - if the response type is not the expected value
      • assertResponseStatusCode

        public static void assertResponseStatusCode​(javax.ws.rs.core.Response response,
                                                    javax.ws.rs.core.Response.Status expectedStatus)
        Asserts the actual response expectedStatus code is expectedStatus.
        Parameters:
        response - the response to check
        expectedStatus - expected response status
        Throws:
        AssertionError - if the response expectedStatus code is not the expected value
      • assertResponseStatusCode

        public static void assertResponseStatusCode​(javax.ws.rs.core.Response response,
                                                    int expectedStatusCode)
        Asserts the actual response status code is expectedStatusCode.
        Parameters:
        response - the response to check
        expectedStatusCode - the expected status code
        Throws:
        AssertionError - if the response status code is not the expected value
      • assertBadRequest

        public static void assertBadRequest​(javax.ws.rs.core.Response response)
        Asserts the specified response has a 400 Bad Request status.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response status is not 400 Bad Request
      • assertConflict

        public static void assertConflict​(javax.ws.rs.core.Response response)
        Asserts the specified response has a 409 Conflict status.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response status is not 409 Conflict
      • assertUnprocessableEntity

        public static void assertUnprocessableEntity​(javax.ws.rs.core.Response response)
        Asserts the specified response has a 422 Unprocessable Entity status.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response is not a 422 Unprocessable Entity
      • assertCreatedResponse

        public static void assertCreatedResponse​(javax.ws.rs.core.Response response)
        Asserts that response is a 201 Created response.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response is not a 201 Created
      • assertCreatedResponseWithLocation

        public static void assertCreatedResponseWithLocation​(javax.ws.rs.core.Response response,
                                                             String expectedLocation)
        Asserts that response is a 201 Created response with the Location header set to expectedLocation.
        Parameters:
        response - the response to check
        expectedLocation - the expected value of the Location header
        Throws:
        AssertionError - if the response isn't a 201 or if the location header is incorrect
      • assertCreatedResponseWithLocationEndingWith

        public static void assertCreatedResponseWithLocationEndingWith​(javax.ws.rs.core.Response response,
                                                                       String locationEndingWith)
        Asserts that response is a 201 Created response with the Location header having a location path ending with locationEndingWith.

        Use this method when you only have a relative location path (e.g. "/object/42") instead of assertCreatedResponseWithLocation(Response, String), which requires a location to match exactly: i.e. "http://localhost:4949/object/42"

        Parameters:
        response - the response to check
        locationEndingWith - the expected substring at the end of the Location header
        Throws:
        AssertionError - if the response isn't a 201 or if the location header is incorrect
      • assertCreatedResponseWithLocation

        public static void assertCreatedResponseWithLocation​(javax.ws.rs.core.Response response,
                                                             String basePath,
                                                             Object id)
        Asserts that response is a 201 Created response with the Location header set to the value of basePath concatenated with id.
        Parameters:
        response - the response to check
        basePath - the base path, e.g. "/users" (do not add a trailing slash)
        id - the identifier, e.g. "42" (do not add a leading slash)
        Throws:
        AssertionError - if the response isn't a 201 or if the location header is incorrect
      • assertOkResponse

        public static void assertOkResponse​(javax.ws.rs.core.Response response)
        Asserts that response is a 200 OK response.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response isn't a 200
      • assertAcceptedResponse

        public static void assertAcceptedResponse​(javax.ws.rs.core.Response response)
        Asserts that response is a 202 Accepted response.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response isn't a 202
      • assertNoContentResponse

        public static void assertNoContentResponse​(javax.ws.rs.core.Response response)
        Asserts that response is a 204 No Content response.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response isn't a 204
      • assertUnauthorizedResponse

        public static void assertUnauthorizedResponse​(javax.ws.rs.core.Response response)
        Asserts that response is a 401 Unauthorized response.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response isn't a 401
      • assertForbiddenResponse

        public static void assertForbiddenResponse​(javax.ws.rs.core.Response response)
        Asserts that response is a 403 Forbidden response.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response isn't a 403
      • assertNotFoundResponse

        public static void assertNotFoundResponse​(javax.ws.rs.core.Response response)
        Asserts that response is a 404 Not Found response.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response isn't a 404
      • assertInternalServerErrorResponse

        public static void assertInternalServerErrorResponse​(javax.ws.rs.core.Response response)
        Asserts that response is a 500 Internal Server Error response.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response isn't a 500
      • assertSuccessfulResponseFamily

        public static void assertSuccessfulResponseFamily​(javax.ws.rs.core.Response response)
        Asserts that response has a 2xx status code.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response isn't a 2xx
      • assertClientErrorResponseFamily

        public static void assertClientErrorResponseFamily​(javax.ws.rs.core.Response response)
        Asserts that response has a 4xx status code.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response isn't a 4xx
      • assertServerErrorResponseFamily

        public static void assertServerErrorResponseFamily​(javax.ws.rs.core.Response response)
        Asserts that response has a 5xx status code.
        Parameters:
        response - the response to check
        Throws:
        AssertionError - if the response isn't a 5xx
      • assertResponseFamily

        public static void assertResponseFamily​(javax.ws.rs.core.Response response,
                                                javax.ws.rs.core.Response.Status.Family expectedFamily)
        Asserts that response is in the expected Response.Status.Family.
        Parameters:
        response - the response to check
        expectedFamily - the expected Response.Status.Family of the given response
        Throws:
        AssertionError - if the response isn't in the expected family
      • assertCustomHeaderFirstValue

        public static void assertCustomHeaderFirstValue​(javax.ws.rs.core.Response response,
                                                        String headerName,
                                                        Object expectedValue)
        Asserts that the response has a custom header named headerName and whose first value is expectedValue.
        Parameters:
        response - the response to check
        headerName - the name of the header to check
        expectedValue - the expected value of the header
        Throws:
        AssertionError - if the response doesn't have a header named headerName or if the value doesn't match
      • assertNonNullResponseEntity

        public static <T> T assertNonNullResponseEntity​(javax.ws.rs.core.Response response,
                                                        Class<T> expectedClass)
        Asserts that the response contains an entity using Response.getEntity() whose class is expectedClass.
        Type Parameters:
        T - the entity type
        Parameters:
        response - the response to check
        expectedClass - the expected entity type
        Returns:
        the response entity, cast as a T
        Throws:
        AssertionError - if the response entity doesn't exist or isn't the expected type
        See Also:
        Response.getEntity()
      • assertResponseEntity

        public static <T> void assertResponseEntity​(javax.ws.rs.core.Response response,
                                                    T expectedEntity)
        Asserts that the response contains an entity using Response.getEntity() that is the same instance as the expectedEntity.
        Type Parameters:
        T - the entity type
        Parameters:
        response - the response to check
        expectedEntity - the expected response entity
        Throws:
        AssertionError - if the if the response entity doesn't exist or isn't the expected object
        See Also:
        Response.getEntity()
      • assertResponseEntity

        public static <T> T assertResponseEntity​(javax.ws.rs.core.Response response,
                                                 Class<T> expectedClass,
                                                 T expectedEntity)
        Asserts that the response contains an entity using Response.getEntity() whose class is expectedClass and which is the same instance as the expectedEntity.
        Type Parameters:
        T - the response entity type
        Parameters:
        response - the response to check
        expectedClass - the expected entity type
        expectedEntity - the expected entity
        Returns:
        the same entity assuming the assertion passed
        Throws:
        AssertionError - if the response entity doesn't exist or isn't the expected class/object
        See Also:
        Response.getEntity()
      • assertEqualResponseEntity

        public static <T> void assertEqualResponseEntity​(javax.ws.rs.core.Response response,
                                                         T expectedEntity)
        Asserts that the response contains an entity using Response.getEntity() that is equal to the expectedEntity using logical equality, e.g. using Object.equals(Object).
        Type Parameters:
        T - the entity type
        Parameters:
        response - the response to check
        expectedEntity - the expected entity
        Throws:
        AssertionError - if the response entity doesn't exist or isn't equal to the expected object
        See Also:
        Response.getEntity()
      • assertMapResponseEntity

        public static Map<String,​Object> assertMapResponseEntity​(javax.ws.rs.core.Response response)
        Asserts that the response contains an entity using Response.getEntity() that is a Map and specifically one whose keys are Strings and values are Objects.
        Parameters:
        response - the response to check
        Returns:
        the response entity cast to a map
        Throws:
        AssertionError - if the response doesn't have an entity, or is not a map
        Implementation Note:
        This method performs an unchecked cast of the map entity to Map<String, Object> after verifying the entity is a Map instance. Unexpected results and/or exceptions may occur if the map does not contain only String keys.