Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > JUnit4 - Distributed JUnit
JUnit4 - Distributed JUnit
Added by morpheus, last edited by morpheus on Oct 22, 2007  (view change)
Labels: 
(None)


Disributed Junit4 Example

This example will demonstrate how GridGain can distribute your long running JUnit4 tests or test suites across grid and hence dramatically speeding up overall execution of all tests.

To try this example you will need to open GridJunit4ExampleSuite.java in IDEA, Eclipse or any other IDE and run this JUnit4 suite using standard IDE JUnit integration. You will observe how execution of the tests is offloaded to remote nodes and then the results are seen in the IDE just as if it was a local run.

Package:
org.gridgain.examples.junit.junit4

There is one class worth noting in this example:

Running Grid Node

To run this example you need to start one or more additional grid nodes. For simplicity, you can start these nodes on the same box on which you are running the example.

To start a remote node open the terminal window on Linux/Mac OS X or Command Prompt on Windows, change directory to ${GRIDGAIN_HOME}/bin and run the gridgain.{sh|bat} script. However, distributed JUnits have to use GridTestExecutorService Javadoc which is pre-configured in ${GRIDGAIN_HOME}/config/junit/junit-spring.xml Spring configuration file. You need to specify a path to this file to the gridgain startup script as follows:

gridgain.bat config/junit/junit-spring.xml

or starting from GridGain 1.6.1, simply execute gridgain-junit.{sh|bat} script.

gridgain-junit.bat

It takes 2-3 seconds for grid node to start and if everything worked fine you should see starting log ending with successful start acknowledgment.

GridJunit4ExampleSuite.java

This class represents a standard JUnit4 suite with standard @RunWith and @SuiteClasses annotations.

Specify GridJunit4Suite Runner

The only difference from standard JUnit4 suites is that instead of specifying Suite runner, we must specify GridJunit4Suite Javadoc runner as follows.

@RunWith(GridJunit4Suite.class)

Running Tests Sequentially

Sometimes it is desired that certain tests run in sequence, yet parallel with other tests. For that you simply need to create a nested suite, then the whole suite will be executed remotely. For example, the following lines of code will guarantee that TestA and TestB always run in sequence.

GridJunit4ExampleNestedSuite.java
@RunWith(Suite.class)
@SuiteClasses({
    TestA.class,
    TestB.class
})
public class GridJunit4ExampleNestedSuite {
    // No-op.
}

Running Tests Locally

Certain tests must run locally no matter what, often due to some environmental issues. Yet these tests can benefit from parallel execution with other tests. GridGain supports it via GridJunit4LocalSuite Javadoc suite runner.

GridJunit4ExampleNestedLocalSuite.java
@RunWith(GridJunit4LocalSuite.class) // Specify local suite to run tests.
@SuiteClasses(TestC.class)
public class GridJunit4ExampleNestedLocalSuite {
    // No-op.
}

Full Source Code.

GridJunit4ExampleSuite.java
@RunWith(GridJunit4Suite.class)
@SuiteClasses({
    GridJunit4ExampleNestedSuite.class, // Nested suite that will execute tests A and B added to it sequentially.
    GridJunit4ExampleNestedLocalSuite.class, // Local suite that will execute its test C locally.
    TestD.class // TestD will run in parallel with (A and B) and C tests.
})
public class GridJunit4ExampleSuite {
    // No-op.
}

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators