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:
- Inject an embedded server before all tests and stop it after all tests.
- Read server configuration annotated with
TestServerConfiguration
- Resolve parameters of type (or any parameters inheriting from):
- Inject class field annotated with:
@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.
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 Summary
ConstructorsConstructorDescriptionCreate the jupiter with default server that will be automatically detected using the Service Provider API.JunitServerExtension
(AbstractConfiguration configuration) Create the jupiter with given server configuration.JunitServerExtension
(EmbeddedServer<?> server) Create the jupiter with given server to start/stop before/after tests. -
Method Summary
Modifier and TypeMethodDescriptionvoid
afterAll
(org.junit.jupiter.api.extension.ExtensionContext context) void
afterEach
(org.junit.jupiter.api.extension.ExtensionContext context) void
beforeAll
(org.junit.jupiter.api.extension.ExtensionContext context) void
beforeEach
(org.junit.jupiter.api.extension.ExtensionContext context) protected EmbeddedServer<?>
instantiateServer
(Class<?> testClass, AbstractConfiguration configuration) Instantiate server (implementation to use is automatically detected using the Service Provider API).resolveParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) boolean
supportsParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
-
Constructor Details
-
JunitServerExtension
public JunitServerExtension()Create the jupiter with default server that will be automatically detected using the Service Provider API. -
JunitServerExtension
Create the jupiter with given server to start/stop before/after tests.- Parameters:
server
- The embedded server to use.- Throws:
NullPointerException
- Ifserver
isnull
.
-
JunitServerExtension
Create the jupiter with given server configuration.- Parameters:
configuration
- The embedded server configuration to use.- Throws:
NullPointerException
- Ifconfiguration
isnull
.
-
-
Method Details
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeAll
in interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
afterAll
in interfaceorg.junit.jupiter.api.extension.AfterAllCallback
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
afterEach
in interfaceorg.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 interfaceorg.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 interfaceorg.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.
-