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 Summary
ConstructorsConstructorDescriptionCreate rule with default embedded server.ServerRule
(AbstractConfiguration configuration) Create rule with embedded server configuration.ServerRule
(EmbeddedServer<?> server) Create rule with an embedded server. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
after()
protected void
before()
Returns HTTP client that can be used against embedded server.getHost()
Get embedded server scheme (should be"localhost"
).getPath()
Get path defined on embedded server.int
getPort()
Get port used by embedded server.Get embedded server scheme (a.k.a"http"
or"https"
).Get embedded server.getUrl()
Get url used to query embedded server.boolean
Check if embedded server is started.void
restart()
Restart embedded server.void
start()
Start embedded server.void
stop()
Stop embedded server.toString()
Methods inherited from class org.junit.rules.ExternalResource
apply
-
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
Create rule with embedded server configuration.Embedded server implementation is chosen using the service provider interface from the JDK.
- Parameters:
configuration
- Server configuration.
-
ServerRule
Create rule with an embedded server.- Parameters:
server
- Embedded server, not null.- Throws:
NullPointerException
- Ifserver
isnull
.
-
-
Method Details
-
before
protected void before()- Overrides:
before
in classorg.junit.rules.ExternalResource
-
after
protected void after()- Overrides:
after
in classorg.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
Get embedded server scheme (a.k.a"http"
or"https"
).- Returns:
- Scheme.
- See Also:
-
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
Get path defined on embedded server.- Returns:
- Path.
- See Also:
-
getUrl
Get url used to query embedded server.- Returns:
- URL.
- See Also:
-
getServer
Get embedded server.- Returns:
- Server.
-
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
-