I have built my own implementation of a stack in Java, which looks something like this:
There is the interface "Stack" which provides the basic functions (pop, push, peek etc.). And then I have 2 concrete classes, one with the help of arrays and the one with a linked list (how is not important in this case).
Now my question: I want to test this with JUnit5 and because you can't instantiate an interface, I have to test each function once for the class with the arrays and once for the class with the linked list, so the code is unnecessarily long. Is there a way that I can test all functions for the interface or something similar? Because if now a third implementation was added, I'd have to rewrite it all again.
I have already tried 'ParameterizedTests', but I have not made any progress.
I would be happy about help!
Test Interfaces would be another possibility. You'd define your tests as default methods of an interface and implement the interface once per Stack implementation. Each implementation can add additional tests etc.
Make a private method which performs the test given the interface type as a parameter
Then call it in a unit test:
I do not know what problem with
@ParameterizedTest
you are facing, but as you requested this is a very generic test example which could be useful for your test: