Class ServerRule

java.lang.Object
org.junit.rules.ExternalResource
com.github.mjeanroy.junit.servers.junit4.ServerRule
All Implemented Interfaces:
org.junit.rules.TestRule

public class ServerRule extends org.junit.rules.ExternalResource
Rule that can be used to start and stop embedded server. This rule is automatically used if the JunitServerRunner is used.

This rule should be used as a ClassRule to start embedded container before all tests and shutdown after all tests. Here is an example automatic classpath detection:


   public class Test {
     @ClassRule
     public static serverRule = ServerRule();

     @Test
     public void it_should_have_a_port() {
       assertTrue(serverRule.getPort() > 0).
     }
   }
 
  • Constructor Details

    • ServerRule

      public ServerRule()
      Create rule with default embedded server.

      Embedded server implementation is chosen using the service provider interface from the JDK.

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

    • ServerRule

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

      Embedded server implementation is chosen using the service provider interface from the JDK.

      Parameters:
      configuration - Server configuration.
    • ServerRule

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

    • before

      protected void before()
      Overrides:
      before in class org.junit.rules.ExternalResource
    • after

      protected void after()
      Overrides:
      after in class org.junit.rules.ExternalResource
    • 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 embedded server.
      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