📜 ⬆️ ⬇️

Java: Testing Web Applications Pages with JWebUnit and Cargo Container

When I was a j2ee programmer, I had to develop websites with a fairly large number of pages that use ajax and other beautiful things for visualization. Controllers could be tested using JUnit and request-based queries / responses. But for a huge number of ftl templates and their JavaScript code, this option was not suitable.



A great opportunity to really test pages is JWebUnit . After adding it to the project, you will have access to the WebTestCase class, which is derived from TestCase from classic Junit.
')
The WebTestCase class provides a high-level API for working with web pages. It’s simply pointless to describe it because of extreme simplicity, so I’ll just give an example of the 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 run a web server before performing a test pack,
and it is even better to run it on the server in a task, for example, for Ant, before building it, and run the tests there, so as not to break the production version, in the case of a commit in a hurry. Cargo Conatiner will help us with this, this is a Java API that helps us manage the Web Server from Java code or directly from an Ant task. By tradition, I will give both usage scenarios that can be found on the developer’s website:

We execute code by 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 )



<property name = "cargolib.dir" value = "$ {basedir} / cargolib" />
<property name = "cargo-uberjar" value = "$ {cargolib.dir} /cargo-core-uberjar-0.7.jar" />
<property name = "cargo-antjar" value = "$ {cargolib.dir} /cargo-ant-0.7.jar" />

<taskdef resource = "cargo.tasks" >
<classpath >
<pathelement location = "$ {cargo-uberjar}" />
<pathelement location = "$ {cargo-antjar}" />
</ classpath >
</ taskdef >

<target name = "cargostart" depends = "war" >
<delete dir = "$ {tomcatconfig.dir}" />
<mkdir dir = "$ {tomcatlog.dir}" />
<mkdir dir = "$ {tomcatconfig.dir}" />
<echo message = "Starting Cargo ..." />
<echo message = "Using tomcat.home = $ {tomcat.home}" />
<echo message = "Using war = $ {mywarfile}" />
<echo message = "Jars used = $ {cargo-uberjar}, $ {cargo-antjar}" />

<cargo containerId = "tomcat5x" home = "$ {tomcat.home}" output = "$ {tomcatlog.dir} /output.log"
log = "$ {tomcatlog.dir} /cargo.log" action = "start" >
<configuration home = "$ {tomcatconfig.dir}" >
<property name = "cargo.servlet.port" value = "8080" />
<property name = "cargo.logging" value = "high" />
<deployable type = "war" file = "$ {mywarfile}" />
</ configuration >
</ cargo >
<! - here you can do anything, such as tests ->
<! - it is necessary to slow down the server in the same way, just specify another action ->
</ target >

Source: https://habr.com/ru/post/70739/


All Articles