Class ServerBackedApplicationUnderTest
- java.lang.Object
- 
- ratpack.test.ServerBackedApplicationUnderTest
 
- 
- All Implemented Interfaces:
- AutoCloseable,- ApplicationUnderTest,- CloseableApplicationUnderTest
 - Direct Known Subclasses:
- MainClassApplicationUnderTest
 
 public abstract class ServerBackedApplicationUnderTest extends Object implements CloseableApplicationUnderTest AnApplicationUnderTestimplementation that manages aRatpackServer.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@Aftermechanism.This class supports Impositions, which can be used to augment the server for testability.
- 
- 
Constructor SummaryConstructors Constructor Description ServerBackedApplicationUnderTest()
 - 
Method SummaryAll Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddDefaultImpositions(ImpositionsSpec impositionsSpec)Adds default impositions, that make sense in most cases.protected voidaddImpositions(ImpositionsSpec impositions)Adds impositions to be imposed on the server while it is being created and starting.voidclose()Delegates tostop().protected ImpositionscreateImpositions()Creates theImpositionsto impose on the server.protected abstract RatpackServercreateServer()Creates the server to be tested.URIgetAddress()Returns the address to the root of the server, starting it if necessary.static ServerBackedApplicationUnderTestof(RatpackServer ratpackServer)Creates a new instance backed by the given server.static ServerBackedApplicationUnderTestof(Factory<? extends RatpackServer> ratpackServer)Creates a new instance backed by the server returned by the given function.voidstop()Stops the server if it is running.- 
Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 - 
Methods inherited from interface ratpack.test.ApplicationUnderTestgetHttpClient
 - 
Methods inherited from interface ratpack.test.CloseableApplicationUnderTesttest
 
- 
 
- 
- 
- 
Method Detail- 
ofpublic static ServerBackedApplicationUnderTest of(RatpackServer ratpackServer) Creates a new instance backed by the given server.- Parameters:
- ratpackServer- the server to test
- Returns:
- an application under test backed by the given server
- Since:
- 1.2
 
 - 
ofpublic static ServerBackedApplicationUnderTest of(Factory<? extends RatpackServer> ratpackServer) Creates a new instance backed by the server returned by the given function.The function is called lazily, the first time the server is needed. - Parameters:
- ratpackServer- the server to test
- Returns:
- an application under test backed by the given server
- Since:
- 1.2
 
 - 
createServerprotected abstract RatpackServer createServer() throws Exception Creates the server to be tested.The server does not need to be started when returned by this method. - Returns:
- the server to test
- Throws:
- Exception- any
 
 - 
createImpositionsprotected Impositions createImpositions() throws Exception Creates theImpositionsto impose on the server.This implementation effectively delegates to addDefaultImpositions(ImpositionsSpec)andaddImpositions(ImpositionsSpec).It is generally more appropriate to override addImpositions(ImpositionsSpec)than this method.- Returns:
- the impositions
- Throws:
- Exception- any
- Since:
- 1.2
 
 - 
addDefaultImpositionsprotected void addDefaultImpositions(ImpositionsSpec impositionsSpec) Adds default impositions, that make sense in most cases.Specifically adds a ForceDevelopmentImpositionwith atruevalue, and aForceServerListenPortImposition.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. - Parameters:
- impositionsSpec- the impositions spec, that impositions can be added to
- Since:
- 1.2
 
 - 
addImpositionsprotected void addImpositions(ImpositionsSpec impositions) Adds impositions to be imposed on the server while it is being created and starting.import ratpack.core.server.RatpackServer; import ratpack.core.impose.ImpositionsSpec; import ratpack.core.impose.ServerConfigImposition; import ratpack.test.MainClassApplicationUnderTest; import static java.util.Collections.singletonMap; import static org.junit.jupiter.api.Assertions.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()) ); } }- Parameters:
- impositions- the spec to add impositions to
- Since:
- 1.2
- See Also:
- Impositions,- ServerConfigImposition,- ForceDevelopmentImposition,- ForceServerListenPortImposition,- UserRegistryImposition
 
 - 
getAddresspublic URI getAddress() Returns the address to the root of the server, starting it if necessary.- Specified by:
- getAddressin interface- ApplicationUnderTest
- Returns:
- the address to the root of the server
 
 - 
stoppublic void stop() Stops the server if it is running.- See Also:
- RatpackServer.stop()
 
 - 
closepublic void close() Delegates tostop().- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- CloseableApplicationUnderTest
 
 
- 
 
-