Skip to content
Advertisement

Run same Junit Test with multiple objects derived from same interface

I am trying to improve my knowledge about testing I’m trying to achieve running the same JUnit test class with different objects derived from the same interface.

so we can assume the following:

JavaScript

So my question is how to run the test class with the objects from A,B,C without falling into the trap of duplicate code and redundancy?

Please note that I wrote this example just to illustrate my point, and the sort() doStuff() methods are just placeholders but when you have over ~70 line of code duplication in each test class it starts to look ugly and redundant.

*I have looked at @beforeEach, @Before, @After and I don’t think I see a way where those might help me.

Advertisement

Answer

You can write a parameterized test with a MethodSource.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement