JUnit 4 Integration

 

JTR is not an alternative to JUnit, being mainly focused on stress-testing back-end systems built around the JEE technological-stack rather than unit-testing single classes’ methods.


That’s why JTR, since version 5, seamlessly integrates JUnit 4 tests. Integration is fundamental since most of the time one has his/her unit-tests written to prove methods correctness along with higher-level tests (integration-tests) that work on a wider sub-system.


How integration works?

There is nothing special you have to do to run your JUnit 4 test classes. Let’s see an example. Suppose this is your JUnit 4 test class:


package jtr.test.junit;


import org.apache.commons.math.random.JDKRandomGenerator;

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Test;

import static org.junit.Assert.*;


public class JUnitRunner {


    int x, y, sum;


    public JUnitRunner() {

    }


    @BeforeClass

    public static void setUpClass() throws Exception {

    }


    @AfterClass

    public static void tearDownClass() throws Exception {

    }


    @Before

    public void setUp() {

        JDKRandomGenerator prng = new JDKRandomGenerator();

        x = prng.nextInt();

        y = prng.nextInt();

        sum = x + y;

    }


    @After

    public void tearDown() {

        x = 0;

        y = 0;

        sum = 0;

    }


    @Test public void sum() {

        assertTrue(x+y==sum);

    }


    @Test public void mul() {

        JDKRandomGenerator prng = new JDKRandomGenerator();

        int res = prng.nextInt();

        assertTrue(x*y==res);

    }

}


In order to have JTR 5 run this JUnit 4 test all you have to do is simply declare it as any other runner in the jtr.xml file just as follows:


<runner runs="1">

    <runner-fqn>jtr.test.junit.JUnitRunner</runner-fqn>

    <instance-count>1</instance-count>

    <parameters-assignment-policy>indexed</parameters-assignment-policy>

    <parameters>

        <param name="sleepTime" value="10" />

    </parameters>

</runner>


The only thing you have to pay attention to is that as per this preliminary release you still need to specify one parameter, the sleepTime.


Current limitations

JUnit 4 test classes cannot take advantage of the majority the features we have seen so far.

Simply stated there is only a one-way communication between JTR 5 and JUnit 4: the former is aware of the latter but not vice-versa.


The only JTR features JUnit 4 test classes can take advantage of are:

  1. -seamless test distribution (via EDiT)

  2. -concurrent test execution (multiple instantiation via the instance-count element)

  3. -iterated test execution (via the runs attribute)




Next step: JTR logging facilities