JUnit Parameterized Tests Processing

253 views Asked by At

I have a question about parameterized tests in JUnit. I am running a test suite with all of my test classes, it is a requirement for my course to have a test suite to run all of my test classes, so I cannot modify that. The issue is that I have a bunch of Entry objects (let's just take this as an object with a unique id starting from 1 and incremented every time a new instance of it is created), and they are being pre-processed by JUnit. On compiling and running my program, I have 9 entries which are declared in the ParamTest class. Within another class (EntryTest) I have one Entry that I have created and it should have an ID of one. However, it has an ID of 10, meaning the 9 entries from the parameterized test class have been created before hand.

My question is, is there anyway to force the ParamTest class not to do any of the pre-processing before the EntryTest class is run or is this impossible. In the suite I have made sure to declare EntryTest before ParamTest. If it's impossible is there anyway I can get around this other than creating separate suites or running the tests separately? I was thinking a public static int to keep track of the ID from the pre-processed amounts but it sounds like an ugly solution.

1

There are 1 answers

0
Duncan Jones On

I think your testing is going to get ugly, fast, unless you have a way of resetting your static class to a known state.

I would recommend you expose a package-private method that allows you to reset the ID value to something specific (e.g. 0).

Tests should be entirely independent of one another, even within the same test class.