public abstract class ServerBackedApplicationUnderTest extends Object implements CloseableApplicationUnderTest
ApplicationUnderTest
implementation that manages a RatpackServer
.
This class can be used in tests to handle starting the server, making HTTP requests to it and shutting it down when done. It is typically used in tests where the actual application is available on the classpath.
Implementations need only provide a createServer()
method.
Closing this application under test will stop the server
.
Users should ensure that objects of this type are closed when done with, to release ports and other resources.
This is typically done in a test cleanup method, such as via JUnit's @After
mechanism.
This class supports Impositions
, which can be used to augment the server for testability.
Constructor and Description |
---|
ServerBackedApplicationUnderTest() |
Modifier and Type | Method and Description |
---|---|
protected void |
addDefaultImpositions(ImpositionsSpec impositionsSpec)
Adds default impositions, that make sense in most cases.
|
protected void |
addImpositions(ImpositionsSpec impositions)
Adds impositions to be imposed on the server while it is being created and starting.
|
void |
close()
Delegates to
stop() . |
protected Impositions |
createImpositions()
Creates the
Impositions to impose on the server. |
protected abstract RatpackServer |
createServer()
Creates the server to be tested.
|
URI |
getAddress()
Returns the address to the root of the server, starting it if necessary.
|
static ServerBackedApplicationUnderTest |
of(Factory<? extends RatpackServer> ratpackServer)
Creates a new instance backed by the server returned by the given function.
|
static ServerBackedApplicationUnderTest |
of(RatpackServer ratpackServer)
Creates a new instance backed by the given server.
|
void |
stop()
Stops the server if it is running.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
test
getHttpClient
public static ServerBackedApplicationUnderTest of(RatpackServer ratpackServer)
ratpackServer
- the server to testpublic static ServerBackedApplicationUnderTest of(Factory<? extends RatpackServer> ratpackServer)
The function is called lazily, the first time the server is needed.
ratpackServer
- the server to testprotected abstract RatpackServer createServer() throws Exception
The server does not need to be started when returned by this method.
Exception
- anyprotected Impositions createImpositions() throws Exception
Impositions
to impose on the server.
This implementation effectively delegates to addDefaultImpositions(ImpositionsSpec)
and addImpositions(ImpositionsSpec)
.
It is generally more appropriate to override addImpositions(ImpositionsSpec)
than this method.
Exception
- anyprotected void addDefaultImpositions(ImpositionsSpec impositionsSpec)
Specifically adds a ForceDevelopmentImposition
with a true
value,
and a ForceServerListenPortImposition.ephemeral()
imposition.
To negate or change the default impositions, simply add a different imposition of the same type in addImpositions(ImpositionsSpec)
.
Doing so will overwrite the existing imposition of the same type, set by this method.
It is generally not necessary to override this method.
impositionsSpec
- the impositions spec, that impositions can be added toprotected void addImpositions(ImpositionsSpec impositions)
import ratpack.server.RatpackServer;
import ratpack.impose.ImpositionsSpec;
import ratpack.impose.ServerConfigImposition;
import ratpack.test.MainClassApplicationUnderTest;
import static java.util.Collections.singletonMap;
import static org.junit.Assert.assertEquals;
public class Example {
public static class App {
public static void main(String[] args) throws Exception {
RatpackServer.start(s -> s
.serverConfig(c -> c
.props(singletonMap("string", "foo"))
.require("/string", String.class)
)
.handlers(c -> c
.get(ctx -> ctx.render(ctx.get(String.class)))
)
);
}
}
public static void main(String[] args) throws Exception {
new MainClassApplicationUnderTest(App.class) {
@Override
protected void addImpositions(ImpositionsSpec impositions) {
impositions.add(ServerConfigImposition.of(c ->
c.props(singletonMap("string", "bar"))
));
}
}.test(testHttpClient ->
assertEquals("bar", testHttpClient.getText())
);
}
}
impositions
- the spec to add impositions toImpositions
,
ServerConfigImposition
,
ForceDevelopmentImposition
,
ForceServerListenPortImposition
,
UserRegistryImposition
public URI getAddress()
getAddress
in interface ApplicationUnderTest
public void stop()
RatpackServer.stop()
public void close()
stop()
.close
in interface AutoCloseable
close
in interface CloseableApplicationUnderTest