ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Taking JUnit Out of the Box
Pages: 1, 2, 3, 4, 5

Then, we need to create an instance of a TestSuite and add a test to it to be run remotely. This is achieved by specifying the JUnit class (with an optional test method), as well as the name of the agent that should run this specific test.

Here is an example of how this is done:

// create a regular TestSuite
TestSuite suite = 
    new TestSuite("Test for org.pisces.testers");
// create a wrapper for a regular case 
RemoteTestCase rtc = 
        ,"someTestMethod", "remote1"); 

// add test case to TestSuite 

The next section of this article will provide an example of a distributed test scenario and the code of its TestSuite.

Example: Concurrent Login Test

Let's say we have a web application. A user logs in, gets a service, and then logs out. We want to make sure that our security module prevents a single user from concurrently logging in on two different computers.

We have created a MyTestCase test case object, as shown before, that has two test methods. The first, testLogin(), makes sure we can log in for the first time, and a second one, testLoginFails(), makes sure that the second login fails.

Here is what a test suite that utilizes Pisces might look like:

public class RemoteTestSuiteExample extends TestSuite {
    public static Test suite() {
        // configure and start the com layer
        JMSRemoteTestCommunicationLayer com = null;
        try {

            com = 
                new JMSRemoteTestCommunicationLayer
                        new MantaConnectionFactory());
        } catch (JMSException e) {
            throw new RuntimeException(e);

        // do the JUnit thing
        TestSuite suite = 
            new TestSuite("Test for org.pisces.testers");
        // run method testLogin in class MyTestCase
        // on remote agent remote1
        RemoteTestCase rtc =  
                "testLogin", "remote1");

        // run method testLoginFails in class MyTestCase
        // on remote agent remote2
        RemoteTestCase rtc1 = 
                "testLoginFails", "remote2");

        return suite;

If all goes well, the remote agent (called remote1) tries to log in and succeeds. When the other agent (called remote2) tries to log in with the same user and password, it fails because the user is already logged in on a different computer.

This test could have been more complex; for example, by adding a testLogOut() method and performing other security checks, but as an example I wanted to keep it simple.

In this example, I have utilized a serverless JMS provider called MantaRay, an open source messaging middleware package that is based on peer-to-peer technology. In the latest release of Pisces, you can find several examples that utilize a JMS communication layer and other examples that use a multicast communication layer.


Bugs are not fun, but with the help of JUnit, a widely used open source framework for automated testing, the task of running multiple test cases becomes much easier. Many projects extend the functionality of JUnit, but up until now, tests always had to be limited to one machine. With the help of Pisces and some "out of the box" thinking, we can now finally create complex and distributed test scenarios.


Amir Shevat is a senior software developer with eight years of experience in computing.

Return to ONJava.com.