Class HttpStub

java.lang.Object
org.stubit.http.HttpStub
All Implemented Interfaces:
AutoCloseable

@NullMarked public class HttpStub extends Object implements AutoCloseable
A HttpStub wraps an HttpServer that is started locally on a dynamic port to serve fixed stubbed responses (Stubbings).

This can be used to simulate (stub) a remote service used in your production code. In order to do so, you need to configure your code to call the HttpStub's address instead of the actual service address.

  • Constructor Details

    • HttpStub

      public HttpStub()
      Creates and starts the HttpStub.
  • Method Details

    • add

      public HttpStub add(Stubbing... stubbings)
      Parameters:
      stubbings - Stubbing(s) to be added
      Returns:
      this
    • reset

      public HttpStub reset()
      Removes all Stubbings.
      Returns:
      this
    • address

      public String address()
      Returns:
      the HttpStub base address
    • receivedRequests

      public List<HttpRequest> receivedRequests(Predicate<HttpRequest> predicate)
      Returns all received request that match the given predicate.
      Parameters:
      predicate - a HttpRequest Predicate
      Returns:
      the first received request that matches the given predicate
    • receivedRequests

      public List<HttpRequest> receivedRequests()
      Returns all received requests.
      Returns:
      all received requests
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable