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
AnApplicationUnderTest
implementation 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@After
mechanism.This class supports
Impositions
, which can be used to augment the server for testability.
-
-
Constructor Summary
Constructors Constructor Description ServerBackedApplicationUnderTest()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method 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 tostop()
.protected Impositions
createImpositions()
Creates theImpositions
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(RatpackServer ratpackServer)
Creates a new instance backed by the given server.static ServerBackedApplicationUnderTest
of(Factory<? extends RatpackServer> ratpackServer)
Creates a new instance backed by the server returned by the given function.void
stop()
Stops the server if it is running.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ratpack.test.ApplicationUnderTest
getHttpClient
-
Methods inherited from interface ratpack.test.CloseableApplicationUnderTest
test
-
-
-
-
Method Detail
-
of
public 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
-
of
public 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
-
createServer
protected 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
-
createImpositions
protected Impositions createImpositions() throws Exception
Creates theImpositions
to 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
-
addDefaultImpositions
protected void addDefaultImpositions(ImpositionsSpec impositionsSpec)
Adds default impositions, that make sense in most cases.Specifically adds a
ForceDevelopmentImposition
with atrue
value, 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
-
addImpositions
protected 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
-
getAddress
public URI getAddress()
Returns the address to the root of the server, starting it if necessary.- Specified by:
getAddress
in interfaceApplicationUnderTest
- Returns:
- the address to the root of the server
-
stop
public void stop()
Stops the server if it is running.- See Also:
RatpackServer.stop()
-
close
public void close()
Delegates tostop()
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseableApplicationUnderTest
-
-