Unit-testing – Unit Tests code duplication

code smelltestingunit testing

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?

Best Answer

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));
  }
}
Related Topic