Search code examples
javajunitjunit4parameterizedparameterized-unit-test

Create multiple parameter sets in one parameterized class (junit)


Currently I have to create a parameterized test class for every method that I want to test with several different inputs. Is there a way to add this together in one file?

Right now there's CalculatorTestAdd.java which has a set of parameters that are used to check if the Add() function works properly. Is there a possbility for me to 'connect' this set to the Add() function and create an additional set meant for the Subtract() method and add this method in the same test class, resulting in one file called CalculatorTest.java?


Solution

  • Yes. There's nothing special you have to do. For every set of value(s) of the parameters, each @Test method is run once, so just have one method test add() and another method test subtract().

    May I also add that the person who is dictating this requirement is misguided. There is little value in dictating certain design patterns "for all cases" - might as well hire trained monkeys.