Unit and Performance testing with JUnit - tooltwist/documentation GitHub Wiki

JUnit tests are used to test Java classes. They can be run from Eclipse, and are also run automatically from Maven. We have JUnit included in the default ToolTwist Workbench install.

When using Maven, the source code for Java packages and classes are usually located under devel/project/src/main/java. JUnit tests to test those class is then stored under devel/project/src/tests/java.

A JUnit source file contains one or more tests, each of which will return a pass or a fail. Annotations in the code specify methods to be called as tests, and methods to be called prior to and after those tests.

Unit Testing

Unit tests should be used where possible to check the code works correctly, without the need for manual intervention. It is particularly important for testing:

  • code that lies off the normal path of execution.
  • code that is liable to break due to it's dependencies on other parts of the application.

If you are using Maven, JUnit can be installed using the following dependency.

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>

Here's the basic format of a JUnit unit test. Note that is has no main(), as the individual methods will be called directly when JUnit is run.

package com.example.foo;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests for {@link Foo}.
 *
 * @author [email protected] (John Doe)
 */
@RunWith(JUnit4.class)
public class FooTest {

    @Test
    public void thisAlwaysPasses() {
    }

    @Test
    public void thisAlsoPasses() {
        int i = 3;
        assertTrue(i == 3);
    }

    @Test
    public void thisAlwaysFails() {
        fail();
    }

    @Test
    @Ignore
    public void thisIsIgnored() {
    }
}

Documentation for the various assertions can be found here and here.

Performance Testing

A great tool for testing is JUnitBenchmarks from CarrotSearch. This tool runs as a standard JUnit test, but runs the tests multiple times and produces pretty graphs with the results.

To run this tool, you first need to add the following dependencies:

<dependency>
	<groupId>com.carrotsearch</groupId>
	<artifactId>junit-benchmarks</artifactId>
	<version>0.7.0</version>
	<scope>test</scope>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.3.172</version>
	<scope>test</scope>
</dependency>

Here's a basic example, but look at the documentation for full details.

import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart;
...

@AxisRange(min = 0, max = 1)
@BenchmarkMethodChart(filePrefix = "benchmark-myStuff")
public class Check_JsonToXml extends AbstractBenchmark {
	    
    private static final int NUM_ROUNDS = 100;
	private static final int NUM_WARMUPS = 5;
	private static final int ITERATIONS = 10;

	@BeforeClass
    public static void setUpBeforeClass() throws Exception {
        ...
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        ...
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @BenchmarkOptions(benchmarkRounds = NUM_ROUNDS, warmupRounds = NUM_WARMUPS)
    @Test
    public void thisIsMyFirstTest() throws IOException {
        ...
    }

    @BenchmarkOptions(benchmarkRounds = NUM_ROUNDS, warmupRounds = NUM_WARMUPS)
    @Test
    public void thisIsMySecondTest() throws IOException {
        ...
    }
}

Run the JUnit tests with the following properties set. In Eclipse, these are set by right clicking on the class and selecting Debug As...->Debug Configurations, then placing the following in VM Arguments on the Arguments tab.

-DTOOLTWIST_HOME=/tooltwist/tooltwist_osx_8.0-beta/tooltwist -Djub.consumers=CONSOLE,H2 -Djub.db.file=benchmark-mystuff

These benchmarks save their results in an H2 database named benchmark-mystuff.h2.db, and generate a graph in benchmark-mystuff.html (as defined by the @BenchmarkMethodChart(filePrefix = "benchmark-myStuff") line.

The database and graphs will record successive runs of the benchmark, which is useful to see improvements as you tweak the system. If you wish to display a graph without history, delete the database file before running the test.

Links

http://junit.org
A tutorial on JUnit
http://labs.carrotsearch.com/junit-benchmarks.html
https://github.com/carrotsearch/junit-benchmarks

--

⚠️ **GitHub.com Fallback** ⚠️