Last updated April 04, 2009 17:58, by verdoso
Feedicon  

Using Wembed inside Ant to perform JUnit tests

Wembed includes a partial Ant script to facilitate using it inside your own Ant scripts. The file can be found at $WEMBED_HOME/build/_tests.xml and in order to use it, follow these steps:

Edit your Ant script to define the required properties and be able to call the utility targets included with Wembed

<!-- Define values to use the Wembed project for the tests -->
<property name="wembed.installed" value=".../wembed" />
<property name="wembed.tests.buildfile" value="${wembed.installed}/build/_tests.xml" />
...
<!-- Define the values required by the Wembed test tasks -->
<property name="wembed-home" value="${wembed.installed}" />
<property name="classes-tests" value="..." />
<property name="lib-tests" value="..." />
<property name="base" value="${build-www}" />
<property name="extra" value="${jar.lib.extra}" />
...

where the properties required by the tasks are:

  • wembed-home is the directory where you installed Wembed.
  • classes-tests is the directory where you have the .class files for your JUnit tests.
  • lib-tests is the directory where you have all the .jar files that are needed to run the tests (JUnit, HTMLUnit...).
  • base is the directory where your have your web application deployed.
  • extra is the directory where you store the .jar files needed for the container that you don't want to include in your WEB-INF/lib. Typically, datasource libraries (C3P0), utility classes (JavaMail), etc.

After this, you can create some targets that call the test-container and -test-class targets from the _tests.xml file:

...
<target name="test" description="Run all tests" depends="deploy">
	<ant target="test-container" antfile="${wembed.tests.buildfile}" />
</target>
...
<target name="test-html" description="Run the HTML/FreeMarker tests">
	<ant target="-test-class" antfile="${wembed.tests.buildfile}">
		<property name="class" value="test.xxx.MyTests" />
	</ant>
</target>
... 

Those targets will ask you interactively which implementation you want to use, but you can provide it directly if you want run the tests in unassisted mode, like this:

...
<target name="test" description="Run all tests" depends="deploy">
	<ant target="test-container" antfile="${wembed.tests.buildfile}">
		<property name="container" value="jetty" />
	</ant>
</target>
...

How to write your test classes?

You probably would want to start by writing a base class that will take care of starting/stopping the embedded container, like this one:

package test.xxx;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.wembed.Configuration;
import org.wembed.ServerLauncher;

public abstract class CoreTest
{

  static ServerLauncher theSL = null;
  static String baseAddress = null;
  static boolean initialised = false;

  @BeforeClass
  public static void oneTimeSetUp() throws Exception
  {
    //
    System.err.println("Setup once");
    // Let's start up the application
    Configuration theConfiguration = new Configuration();
    theSL = new ServerLauncher(theConfiguration);
    theSL.start();
    baseAddress = "http://localhost:" + theConfiguration.getPort()
        + theConfiguration.getContext();
    // Do other initializations that have to be performed once
    initialised = true;
  }

  @Before
  public void setUp() throws Exception
  {
    if (!initialised)
    {
      throw new Exception(
          "Initialisation failed! It makes no sense to run the tests");
    }
    // Do the initializations that have to be performed before
    // each individual test
  }

  @After
  public void tearDown() throws Exception
  {
    // Cleaning tasks after each individual test
  }

  @AfterClass
  public static void oneTimeTearDown() throws Exception
  {
    theSL.stop();
    // Other cleaning tasks to be performed once, at the end
  }
}

You can then extend this class to create individual sets of tests. For example, this is a class that uses HTMLUnit to see if the home page of the application works and has the proper title:

package test.xxx;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlListItem;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class MyTests extends CoreTest
{
  @Test
  public void checkHomePage() throws Exception
  {
    System.out.println("Checking HTML is reachable...");
    final WebClient webClient = new WebClient();
    final HtmlPage page = (HtmlPage) webClient.getPage(baseAddress);
    //
    assertEquals("This is the home page", page.getTitleText());
    System.out.println("...done!");
  }
}

Each method that you annotate with @Test will be run as a JUnit test, and the Wembed engine will be started ONCE per class. This way you can control exactly if you want to start the container between tests or have them all performed in the same run. Check the HTMLUnit manual for more information and examples.

If you want to see some real examples, you can start with the simple examples that are used to test the same Wembed project and can be found here: Wembed test sources. After that, you might also want to check the, more numerous and complex, tests that are performed in the 'WebLEAFTest project, their source code is at the WebLEAFTest FishEye repository, test classes.

  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close