Class MockConsole

All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class MockConsole extends PrintStream
A capturing PrintStream that allows you to easily capture console output.

Stores output into an internal ByteArrayOutputStream. Note that this means you could run into memory constraints if you heavily use this class.

Typically used in conjunction with the RestClient.Builder.console(PrintStream) to capture console output for testing purposes.

Example:

// A simple REST API that echos a posted bean. @Rest public class MyRest extends BasicRestObject { @RestPost("/bean") public Bean postBean(@Content Bean bean) { return bean; } } // Our mock console. MockConsole console = MockConsole.create(); // Make a call against our REST API and log the call. MockRestClient .create(MyRest.class) .json5() .logRequests(DetailLevel.FULL, Level.SEVERE) .logToConsole() .console(console) .build() .post("/bean", bean) .run(); console.assertContents().is( "", "=== HTTP Call (outgoing) ======================================================", "=== REQUEST ===", "POST http://localhost/bean", "---request headers---", " Accept: application/json5", "---request entity---", " Content-Type: application/json5", "---request content---", "{f:1}", "=== RESPONSE ===", "HTTP/1.1 200 ", "---response headers---", " Content-Type: application/json", "---response content---", "{f:1}", "=== END =======================================================================", "" );

See Also:
  • Constructor Details

  • Method Details

    • create

      public static MockConsole create()
      Creator.
      Returns:
      A new MockConsole object.
    • reset

      public MockConsole reset()
      Resets the contents of this buffer.
      Returns:
      This object.
    • assertContents

      Allows you to perform fluent-style assertions on the contents of this buffer.
      Example:

      MockConsole console = MockConsole.create(); MockRestClient .create(MyRest.class) .console(console) .debug() .json5() .build() .get("/url") .run(); console.assertContents().isContains("HTTP GET /url");

      Returns:
      A new fluent-style assertion object.
    • assertSize

      Allows you to perform fluent-style assertions on the size of this buffer.
      Example:

      MockConsole console = MockConsole.create(); MockRestClient .create(MyRest.class) .console(console) .debug() .json5() .build() .get("/url") .run(); console.assertSize().isGreaterThan(0);

      Returns:
      A new fluent-style assertion object.
    • toString

      public String toString()
      Returns the contents of this buffer as a string.
      Overrides:
      toString in class Object