The question is published on by Tutorial Guruji team.
I have an interface
List whose implementations include Singly Linked List, Doubly, Circular etc. The unit tests I wrote for Singly should do good for most of Doubly as well as Circular and any other new implementation of the interface. So instead of repeating the unit tests for every implementation, does JUnit offer something inbuilt which would let me have one JUnit test and run it against different implementations?
Using JUnit parameterized tests I can supply different implementations like Singly, doubly, circular etc but for each implementation the same object is used to execute all the tests in the class.
With JUnit 4.0+ you can use parameterized tests:
@RunWith(value = Parameterized.class)annotation to your test fixture
- Create a
public staticmethod returning
Collection, annotate it with
@Parameters, and put
CircularList.class, etc. into that collection
- Add a constructor to your test fixture that takes
public MyListTest(Class cl), and store the
Classin an instance variable
- In the
List testList = (List)listClass.newInstance();
With the above setup in place, the parameterized runner will make a new instance of your test fixture
MyListTest for each subclass that you provide in the
@Parameters method, letting you exercise the same test logic for every subclass that you need to test.