Package ratpack.test

Class MainClassApplicationUnderTest

  • All Implemented Interfaces:
    java.lang.AutoCloseable, ApplicationUnderTest, CloseableApplicationUnderTest

    public class MainClassApplicationUnderTest
    extends ServerBackedApplicationUnderTest
    An application under test fixture that can be used to test a server started by a “main” method.
    
     import ratpack.core.server.RatpackServer;
     import ratpack.test.MainClassApplicationUnderTest;
    
     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
             .handlers(c -> c
               .get(ctx -> ctx.render("Hello world!"))
             )
           );
         }
       }
    
       public static void main(String[] args) throws Exception {
         new MainClassApplicationUnderTest(App.class).test(testHttpClient ->
           assertEquals("Hello world!", testHttpClient.getText())
         );
       }
     }
     

    Note that this type implements CloseableApplicationUnderTest, and should be closed when no longer needed.

    See Also:
    ServerBackedApplicationUnderTest.addImpositions(ImpositionsSpec), ServerBackedApplicationUnderTest
    • Constructor Detail

      • MainClassApplicationUnderTest

        public MainClassApplicationUnderTest​(java.lang.Class<?> mainClass)
        Creates a new app under test, based on the given main class.
        Parameters:
        mainClass - a class who's main method starts a Ratpack server
    • Method Detail

      • createServer

        protected ratpack.core.server.RatpackServer createServer()
                                                          throws java.lang.Exception
        Starts the Ratpack server by invoking the public static void main(String[]) method of the “main class” backing this object.
        Specified by:
        createServer in class ServerBackedApplicationUnderTest
        Returns:
        the Ratpack server created by the main method
        Throws:
        java.lang.Exception - if the main method cannot be invoked