Java: Testing Web Application Pages with JWebUnit and Cargo Container

    When I was a j2ee programmer, I had to develop sites with a sufficiently large number of pages, using ajax and other beautiful things for visualization. The controllers could be tested using JUnit and moq requests / responses. But for a huge number of ftl templates and their JavaScript code, this option was not suitable.



    A great opportunity to test the pages for real is JWebUnit . After adding it to the project, you will have access to the WebTestCase class, which is the descendant of TestCase from the classic Junit.

    The WebTestCase class provides a high-level API for working with web pages, it is simply pointless to describe it because of the beyond simplicity, so I will simply give an example of code from the main page:

    public class ExampleWebTestCase extends WebTestCase {
        public void setUp () {
            super.setUp ();
            setBaseUrl (" localhost : 8080 / test");
        }
     
        public void test1 () {
            beginAt ("/ home");
            clickLink ("login");
            assertTitleEquals ("Login");
            setTextField ("username", "test");
            setTextField ("password", "test123");
            submit ();
            assertTitleEquals ("Welcome, test!");
        }
    }
     


    Now it would be nice to learn how to start the web server before executing the test pack,
    and even better, run it on the server in a task, for example, for Ant, before assembling, and run the tests there so as not to break the production version, in case of a quick whip. Cargo Conatiner will help us with this , this Java API helps us to manage the WebServer from Java code or directly from the Ant task. By tradition, I will give both usage scenarios that can be found on the developer's site: We

    execute the code with our hands

    Deployable war = new WAR ("path / to / simple.war");
     
    LocalConfiguration configuration =
        new Resin3xStandaloneLocalConfiguration ("target / myresin3x");
    configuration.addDeployable (war);
     
    InstalledLocalContainer container =
        new Resin3xInstalledLocalContainer (configuration);
    container.setHome ("c: /apps/resin-3.0.18");
     
    container.start ();
    // Here you are assured the container is started.
     
    container.stop ();
    // Here you are assured the container is stopped.
     


    Run in ant task ( Maven 1 , Maven 2 )

    • tomcat.home - Tomato home folder
    • tomcatlog.dir - Logs
    • tomcatconfig.dir - The folder where the container will generate the logs
    • pathtowarfile - The full path to the war file





     

      
        
        
      


     

      
      
      
      
      
      
      
     
             log = "$ {tomcatlog.dir} /cargo.log" action = "start">
        
          
          
          
        
      
     
     

     

    Also popular now: