Package ratpack.test

Class ServerBackedApplicationUnderTest

  • All Implemented Interfaces:
    java.lang.AutoCloseable, ApplicationUnderTest, CloseableApplicationUnderTest
    Direct Known Subclasses:
    MainClassApplicationUnderTest

    public abstract class ServerBackedApplicationUnderTest
    extends java.lang.Object
    implements CloseableApplicationUnderTest
    An 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.

    See Also:
    MainClassApplicationUnderTest, addImpositions(ImpositionsSpec)
    • Constructor Detail

      • ServerBackedApplicationUnderTest

        public ServerBackedApplicationUnderTest()
    • Method Detail

      • of

        public static ServerBackedApplicationUnderTest of​(ratpack.server.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​(ratpack.func.Factory<? extends ratpack.server.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 ratpack.server.RatpackServer createServer()
                                                              throws java.lang.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:
        java.lang.Exception - any
      • addDefaultImpositions

        protected void addDefaultImpositions​(ratpack.impose.ImpositionsSpec impositionsSpec)
        Adds default impositions, that make sense in most cases.

        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.

        Parameters:
        impositionsSpec - the impositions spec, that impositions can be added to
        Since:
        1.2
      • addImpositions

        protected void addImpositions​(ratpack.impose.ImpositionsSpec impositions)
        Adds impositions to be imposed on the server while it is being created and starting.
        
         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) {
               {@literal @}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 java.net.URI getAddress()
        Returns the address to the root of the server, starting it if necessary.
        Specified by:
        getAddress in interface ApplicationUnderTest
        Returns:
        the address to the root of the server
      • stop

        public void stop()
        Stops the server if it is running.
        See Also:
        RatpackServer.stop()