public class ServerRule
extends org.junit.rules.ExternalResource
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 and Description |
---|
ServerRule()
Create rule with default embedded server.
|
ServerRule(AbstractConfiguration configuration)
Create rule with embedded server configuration.
|
ServerRule(EmbeddedServer<?> server)
Create rule with an embedded server.
|
Modifier and Type | Method and Description |
---|---|
protected void |
after() |
protected void |
before() |
HttpClient |
getClient()
Returns HTTP client that can be used against embedded server.
|
String |
getHost()
Get embedded server scheme (should be
"localhost" ). |
String |
getPath()
Get path defined on embedded server.
|
int |
getPort()
Get port used by embedded server.
|
String |
getScheme()
Get embedded server scheme (a.k.a
"http" or "https" ). |
EmbeddedServer<?> |
getServer()
Get embedded server.
|
String |
getUrl()
Get url used to query embedded server.
|
boolean |
isStarted()
Check if embedded server is started.
|
void |
restart()
Restart embedded server.
|
void |
start()
Start embedded server.
|
void |
stop()
Stop embedded server.
|
String |
toString() |
public ServerRule()
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.
public ServerRule(AbstractConfiguration configuration)
Embedded server implementation is chosen using the service provider interface from the JDK.
configuration
- Server configuration.public ServerRule(EmbeddedServer<?> server)
server
- Embedded server, not null.NullPointerException
- If server
is null
.protected void before()
before
in class org.junit.rules.ExternalResource
protected void after()
after
in class org.junit.rules.ExternalResource
public void start()
EmbeddedServer.start()
public void stop()
EmbeddedServer.stop()
public void restart()
EmbeddedServer.restart()
public boolean isStarted()
true
if embedded server is started, false
otherwise.EmbeddedServer.isStarted()
public String getScheme()
"http"
or "https"
).EmbeddedServer.getScheme()
public String getHost()
"localhost"
).EmbeddedServer.getHost()
public int getPort()
Note that:
EmbeddedServer.getPort()
public String getPath()
EmbeddedServer.getPath()
public String getUrl()
EmbeddedServer.getUrl()
public EmbeddedServer<?> getServer()
public HttpClient getClient()
UnsupportedOperationException
- If the client cannot be returned because of missing implementation.Copyright © 2019. All rights reserved.