Class MockRestClient

java.lang.Object
org.apache.juneau.rest.mock.MockRestClient
All Implemented Interfaces:
Closeable, AutoCloseable

public final class MockRestClient extends Object implements Closeable
Next-generation mock REST client that routes requests in-process to a Juneau RestContext.

Use this class to write serverless unit tests for @Rest-annotated resource classes. Requests are dispatched directly to the RestContext without any network activity, making tests fast and self-contained.

Example:

@Rest public class MyResource { @RestGet("/hello") public String hello() { return "Hello!"; } } @Test void test() throws Exception { try (client = MockRestClient.create(MyResource.class)) { var body = client.get("/hello").run().asString(); assertEquals("\"Hello!\"", body); } }

See Also:
Since:
9.2.1
  • Method Details

    • create

      public static MockRestClient create(Object impl)
      Creates an MockRestClient backed by the given REST bean or bean class.

      RestContext instances are cached per class for performance when a Class is passed; a supplied bean instance always gets its own context.

      Parameters:
      impl - The REST bean or bean class annotated with @Rest. If a Class, it must have a no-arg constructor.
      Returns:
      A new client. Never null.
    • builder

      public static MockRestClient.Builder builder(Object impl)
      Returns a new MockRestClient.Builder for the given REST bean or bean class.
      Parameters:
      impl - The REST bean or bean class annotated with @Rest. If a Class, it must have a no-arg constructor.
      Returns:
      A new builder. Never null.
    • get

      public RestRequest get(String path)
      Creates a GET request to the given path.
      Parameters:
      path - The request path (relative to the resource's servlet path). Must not be null.
      Returns:
      A new RestRequest. Never null.
    • post

      public RestRequest post(String path)
      Creates a POST request to the given path.
      Parameters:
      path - The request path. Must not be null.
      Returns:
      A new RestRequest. Never null.
    • put

      public RestRequest put(String path)
      Creates a PUT request to the given path.
      Parameters:
      path - The request path. Must not be null.
      Returns:
      A new RestRequest. Never null.
    • patch

      public RestRequest patch(String path)
      Creates a PATCH request to the given path.
      Parameters:
      path - The request path. Must not be null.
      Returns:
      A new RestRequest. Never null.
    • delete

      public RestRequest delete(String path)
      Creates a DELETE request to the given path.
      Parameters:
      path - The request path. Must not be null.
      Returns:
      A new RestRequest. Never null.
    • head

      public RestRequest head(String path)
      Creates a HEAD request to the given path.
      Parameters:
      path - The request path. Must not be null.
      Returns:
      A new RestRequest. Never null.
    • request

      public RestRequest request(String method, String path)
      Creates a request with the given HTTP method and path.
      Parameters:
      method - The HTTP method. Must not be null.
      path - The request path. Must not be null.
      Returns:
      A new RestRequest. Never null.
    • getClient

      Returns the underlying RestClient.
      Returns:
      The client. Never null.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException