Package ratpack.test

Class ServerBackedApplicationUnderTest

    • Constructor Detail

      • ServerBackedApplicationUnderTest

        public ServerBackedApplicationUnderTest()
    • 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 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​(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​(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) {
               @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