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
TestServerConfiguration
@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:
static, the server will be started before all tests
and stopped after all tests (the recommended way).
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 and Description |
|---|
JunitServerExtension()
Create 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.
|
| Modifier and Type | Method and Description |
|---|---|
void |
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).
|
Object |
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) |
public JunitServerExtension()
public JunitServerExtension(EmbeddedServer<?> server)
server - The embedded server to use.NullPointerException - If server is null.public JunitServerExtension(AbstractConfiguration configuration)
configuration - The embedded server configuration to use.NullPointerException - If configuration is null.public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallbackpublic void afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
afterAll in interface org.junit.jupiter.api.extension.AfterAllCallbackpublic void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallbackpublic void afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
afterEach in interface org.junit.jupiter.api.extension.AfterEachCallbackpublic boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext)
throws org.junit.jupiter.api.extension.ParameterResolutionException
supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolverorg.junit.jupiter.api.extension.ParameterResolutionExceptionpublic Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolverorg.junit.jupiter.api.extension.ParameterResolutionExceptionprotected EmbeddedServer<?> instantiateServer(Class<?> testClass, AbstractConfiguration configuration)
testClass - The test class instance.configuration - The embedded server configuration to use.Copyright © 2019. All rights reserved.