I am thinking about code reuse here:
Assuming we have such classes:
public abstract class AbstractMap {
public abstract void put(K k, V v);
public abstract V get(K k);
}
public class ConcreteMapA extends AbstractMap {
@Override
public void put(K k, V v) {
// put implementation detail A
...
}
@Override
public V get(K k) {
// get implementation detail A
...
}
}
public class ConcreteMapB extends AbstractMap {
@Override
public void put(K k, V v) {
// put implementation detail B
...
}
@Override
public V get(K k) {
// get implementation detail B
...
}
}
The two maps are implementing the same functionalities but with different implementation details. I would like to write unit test for these two concrete classes. But then the test cases start to be duplicate.
public class ConcreteMapATest {
@Test
public void testPutCase1 {
...
}
@Test
public void testPutCase2 {
...
}
@Test
public void testGetCase1 {
...
}
...
}
// following starts to be duplicate code
public class ConcreteMapBTest {
@Test
public void testPutCase1 {
...
}
@Test
public void testPutCase2 {
...
}
@Test
public void testGetCase1 {
...
}
...
}
So what could be the right way to test such? How to reuse these testMethods? Or maybe I just should not do the reuse for unit testing?
Thanks!