Junit: @Parameters method is ran before any other test

137 Views Asked by At

I have a regular parametrized test class, done with JUnit 4.12 (also tested 4.13.1).

@RunWith(Parameterized.class)
public class MyTest {

  @Test
  public void myTest() ...

  @Parameters
  public static Collection<Object[]> data() { ...}

  public MyTest(...)

In the application, there is a design issue that I cannot fix (I mean not now because too much impact). There are some static attributes in other classes, e.g.

public class StaticClass {
  public static Map<String, String> staticMap = new HashMap<>();

MyTest.data() add some data to this staticMap (which is bad, but I have no choice). But some other tests also use this attribute, they add data and then clean it.

I noticed in debug mode that, when I run the tests (at least from intellij), it:

  • call the MyTest.data() at the very beginning (which is strange): data is added to staticMap,
  • it runs a bunch of tests, that will use and clean staticMap
  • when it runs MyTest, then necessary data has been cleaned.

It looks like a strange behavior, I'd expect the @Parameters methods to be executed when the test class is initiated.

I also tested with JUnitParamsRunner (with a provider method and also a provider class) and I have the same behavior.

Is there any way to bypass this behavior?

0

There are 0 best solutions below