How can I avoid code duplication in unit tests?
Using Java and JUnit suppose I have something like this:
public interface Arithmetic<T> {
public T add(T a, T b);
public T sub(T a, T b);
}
public void IntegerArithmetic<Integer> {
//
}
public void DoubleArithmetic<Double> {
//
}
And the test class:
public class TestArith {
private Arithmetic<Integer> arith;
@Before
public void init() {
Arithmetic<Integer> arith = new IntegerArithmetic();
Integer term1 = 4;
Integer term2 = 3;
}
@Test
public void testAdd() {
assertEquals(7, arith.add(term1, term2));
}
@Test
public void testAdd() {
assertEquals(1, arith.sub(term1, term2));
}
}
The problem is that the test class for the DoubleArithmetic class should look exactly the same; the only difference is in @Before init() where the initialization of the concrete type is done. How can this code duplication be avoided?
5
Declare class TestArith as abstract. Then create two classes, TestIntegerArith and TestDoubleArith extending TestArith. In the TestArith create an abstract method getArithmetic, which returns an instance of Arithmetic. Then change the tests to get Arithmetic instance by calling getArithmetic.
So it would look like this:
public abstract class TestArith<T> {
protected abstract Arithmetic<T> getArithmetic();
@Test
public void testAdd() {
assertEquals(7, getArithmetic().add(term1, term2));
}
}
1
This is not always so.
Obviously, tests for 10 / 4
or 1 << 31 * 5
will give different results in IntegerArithmetic
and DoubleArithmetic
.
If there’s a large subset where IntegerArithmetic
and DoubleArithmetic
should act the same, try instantiating a class based on a common interface, e.g. Arithmetic<java.lang.Number>
.
Let me elaborate a bit.
There are two places where code duplication may arise: the initialization, which only differs by the class being tested, and the test cases.
Common tests can go into a common case which can be subclassed, and each subclass’s .setUp()
will instantiate its own concrete instance of Arithmetic
to test.
Class-specific tests get their own test cases.
Initialization code can be de-duplicated by using a parametrized base class from which concrete instances of TestCase
will arise, as @Skillwired proposes.
2