Class JunitServerExtension

java.lang.Object
com.github.mjeanroy.junit.servers.jupiter.JunitServerExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver

public class JunitServerExtension extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.ParameterResolver
Extension for Junit Jupiter. This jupiter will: Note that it is strongly recommended to use parameter instead instead of class injection. For example:

  @ExtendWith(JunitServerExtension.class)
  public class MyTest {

    @Test
    void testGET(HttpClient client) {
      HttpResponse rsp = client.prepareGet("/path")
        .acceptJson()
        .execute();

      Assertions.assertTrue(rsp.status() == 200);
    }
  }
 
The extension may also be used with RegisterExtension, in this case you can use it in two ways:
  • If the extension is declared as static, the server will be started before all tests and stopped after all tests (the recommended way).
  • If the extension is not declared as static, the server will be started before each test and stopped after each test.
For example:

 public class MyTest {

   // The `static` here means that the server will be started before all tests
   // and stopped after all tests.
   // Remove the `static` keyword to start/stop server before/after each test (not recommended).
   @RegisterExtension
   static JunitServerExtension extension = new JunitServerExtension();

   @Test
   void testGET(HttpClient client) {
     HttpResponse rsp = client.prepareGet("/path")
       .acceptJson()
       .execute();

     Assertions.assertTrue(rsp.status() == 200);
   }
 }
 
  • Constructor Details

    • JunitServerExtension

      public JunitServerExtension()
      Create the jupiter with default server that will be automatically detected using the Service Provider API.
    • JunitServerExtension

      public JunitServerExtension(EmbeddedServer<?> server)
      Create the jupiter with given server to start/stop before/after tests.
      Parameters:
      server - The embedded server to use.
      Throws:
      NullPointerException - If server is null.
    • JunitServerExtension

      public JunitServerExtension(AbstractConfiguration configuration)
      Create the jupiter with given server configuration.
      Parameters:
      configuration - The embedded server configuration to use.
      Throws:
      NullPointerException - If configuration is null.
  • Method Details

    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
    • afterAll

      public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • instantiateServer

      protected EmbeddedServer<?> instantiateServer(Class<?> testClass, AbstractConfiguration configuration)
      Instantiate server (implementation to use is automatically detected using the Service Provider API).
      Parameters:
      testClass - The test class instance.
      configuration - The embedded server configuration to use.
      Returns:
      The embedded server.