Interface MkContainer

All Superinterfaces:
AutoCloseable, Closeable
All Known Implementing Classes:
MkGrizzlyContainer

public interface MkContainer extends Closeable
Mock version of Java Servlet container.

A convenient tool to test your application classes against a web service. For example:

 MkContainer container = new MkGrizzlyContainer()
   .next(new MkAnswer.Simple(200, "works fine!"))
   .start();
 new JdkRequest(container.home())
   .header("Accept", "text/xml")
   .fetch().as(RestResponse.class)
   .assertStatus(200)
   .assertBody(Matchers.equalTo("works fine!"));
 MatcherAssert.assertThat(
   container.take().method(),
   Matchers.equalTo("GET")
 );
 container.stop();

Keep in mind that container automatically reserves a new free TCP port and works until JVM is shut down. The only way to stop it is to call stop().

Since version 0.11 container implements Closeable and can be used in try-with-resource block.

Since:
0.10
See Also:
  • Method Details

    • next

      MkContainer next(MkAnswer answer)
      Give this answer on the next request.
      Parameters:
      answer - Next answer to give
      Returns:
      This object
    • next

      MkContainer next(MkAnswer answer, org.hamcrest.Matcher<MkQuery> condition)
      Give this answer on the next request if the matcher condition is satisfied.
      Parameters:
      answer - Next answer to give
      condition - The condition to match
      Returns:
      This object
    • next

      MkContainer next(MkAnswer answer, org.hamcrest.Matcher<MkQuery> condition, int count)
      Give this answer on the next request(s) if the matcher condition is satisfied up to a certain number of requests.
      Parameters:
      answer - Next answer to give
      condition - The condition to match
      count - Number of requests to match
      Returns:
      This object
    • take

      MkQuery take()
      Get the oldest request received (NoSuchElementException if no more elements in the list).
      Returns:
      Request received
    • take

      MkQuery take(org.hamcrest.Matcher<MkAnswer> matcher)
      Get the oldest request received subject to the matching condition. (NoSuchElementException if no elements satisfy the condition).
      Parameters:
      matcher - The matcher specifying the condition
      Returns:
      Request received satisfying the matcher
    • takeAll

      Collection<MkQuery> takeAll(org.hamcrest.Matcher<MkAnswer> matcher)
      Get the all requests received satisfying the given matcher. (NoSuchElementException if no elements satisfy the condition).
      Parameters:
      matcher - The matcher specifying the condition
      Returns:
      Collection of all requests satisfying the matcher, ordered from oldest to newest.
    • queries

      int queries()
      How many queries we have left.
      Returns:
      Total number of queries you can retrieve with take()
      Since:
      1.0
    • start

      MkContainer start() throws IOException
      Start it on the first available TCP port.
      Returns:
      This object
      Throws:
      IOException - If fails
    • start

      MkContainer start(int prt) throws IOException
      Start it on a provided port.
      Parameters:
      prt - The port where it should start listening
      Returns:
      This object
      Throws:
      IOException - If fails
    • stop

      void stop()
      Stop container.
    • home

      URI home()
      Get its home.
      Returns:
      URI of the started container