Class EmbeddedServerRunner

java.lang.Object
com.github.mjeanroy.junit.servers.engine.EmbeddedServerRunner
All Implemented Interfaces:
TestRunner

public final class EmbeddedServerRunner extends Object
An engine that will handle the embedded server lifecycle:
  1. Start embedded server before running any tests.
  2. Stop embedded server after all tests are run.
  • Constructor Details

    • EmbeddedServerRunner

      public EmbeddedServerRunner()
      Create rule with default embedded server.

      Embedded server implementation is chosen using classpath detection: jetty or tomcat will be instantiate if implementation is available on classpath (it means if sub-module is imported, it should be enough to instantiate embedded server).

      The server will automatically use the default configuration, to specify a custom configuration, use EmbeddedServerRunner(AbstractConfiguration) constructor.

    • EmbeddedServerRunner

      public EmbeddedServerRunner(AbstractConfiguration configuration)
      Create rule with embedded server configuration.

      Embedded server implementation is chosen using classpath detection: jetty or tomcat will be instantiate if implementation is available on classpath (it means if sub-module is imported, it should be enough to instantiate embedded server !).

      Parameters:
      configuration - Server configuration.
    • EmbeddedServerRunner

      public EmbeddedServerRunner(EmbeddedServer<?> server)
      Create rule with an embedded server.
      Parameters:
      server - Embedded server, not null.
      Throws:
      NullPointerException - If server is null.
  • Method Details

    • beforeAll

      public void beforeAll()
      Run the BEFORE step of the lifecycle test.
      Specified by:
      beforeAll in interface TestRunner
    • afterAll

      public void afterAll()
      Run the AFTER step of the lifecycle test.
      Specified by:
      afterAll in interface TestRunner
    • start

      public void start()
      Start embedded server.
      See Also:
    • stop

      public void stop()
      Stop embedded server.
      See Also:
    • restart

      public void restart()
      Restart embedded server.
      See Also:
    • isStarted

      public boolean isStarted()
      Check if embedded server is started.
      Returns:
      true if embedded server is started, false otherwise.
      See Also:
    • getScheme

      public String getScheme()
      Get embedded server scheme (a.k.a "http" or "https").
      Returns:
      Scheme.
      See Also:
    • getHost

      public String getHost()
      Get embedded server scheme (should be "localhost").
      Returns:
      Host.
      See Also:
    • getPort

      public int getPort()
      Get port used by embedded server.

      Note that:

      • If the server is not started, the returned port is the one set in the configuration.
      • Otherwise, the "real" port is returned (the port used by the embedded server)
      Returns:
      The port.
      See Also:
    • getPath

      public String getPath()
      Get path defined on embedded server.
      Returns:
      Path.
      See Also:
    • getUrl

      public String getUrl()
      Get url used to query embedded server.
      Returns:
      URL.
      See Also:
    • getServer

      public EmbeddedServer<?> getServer()
      Get embedded server.
      Returns:
      Server.
    • getClient

      public HttpClient getClient()
      Returns HTTP client that can be used against server.
      Returns:
      The HTTP client.
      Throws:
      UnsupportedOperationException - If the client cannot be returned because of missing implementation.
    • getClient

      public HttpClient getClient(HttpClientConfiguration configuration)
      Returns HTTP client that can be used against server.
      Parameters:
      configuration - The client configuration.
      Returns:
      The HTTP client.
      Throws:
      UnsupportedOperationException - If the client cannot be returned because of missing implementation.
    • getClient

      public HttpClient getClient(HttpClientStrategy strategy)
      Returns HTTP client that can be used against server.
      Parameters:
      strategy - The strategy to use.
      Returns:
      The HTTP client.
      Throws:
      UnsupportedOperationException - If the client cannot be returned because of missing implementation.
    • getClient

      public HttpClient getClient(HttpClientStrategy strategy, HttpClientConfiguration configuration)
      Returns HTTP client that can be used against server.
      Parameters:
      strategy - The strategy to use.
      configuration - The client configuration.
      Returns:
      The HTTP client.
      Throws:
      UnsupportedOperationException - If the client cannot be returned because of missing implementation.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • beforeEach

      public void beforeEach(Object target)
      Description copied from interface: TestRunner
      Method called before running unit test.
      Specified by:
      beforeEach in interface TestRunner
      Parameters:
      target - The test class instance.
    • afterEach

      public void afterEach(Object target)
      Description copied from interface: TestRunner
      Method called after running unit test.
      Specified by:
      afterEach in interface TestRunner
      Parameters:
      target - The test class instance.