4

why parameterized tests are executed before setUp function?

    @Before
    public void setUp(){
        System.out.println("some logic");
    }


    @ParameterizedTest
    @CsvSource({"1997"})
    void myTest(String arg) {
        System.out.println(arg);
    }
kingGarfield
  • 304
  • 1
  • 12
  • 1
    can you add the import statements? I think Before should be BeforeAll or BeforeEach. Probably setUp is not called at all. – aeberhart Aug 02 '20 at 12:04
  • 1
    you,re right. BeforeEach is working but Before is called after my tests. they are're not exactly the same. – kingGarfield Aug 02 '20 at 12:43

1 Answers1

4

Relating to @aeberhart's comment, there is no @Before annotation in JUnit 5, if that's what you're using as a test runner. You need to use @BeforeAll, which in is the same as JUnit 4's @BeforeClass.

tomdaly
  • 467
  • 2
  • 11
  • I spent a day trying to find out why the mocks used in my parameterized test were null. Turns out the `initMocks` was being called in the setup method annotated with `@Before` and it wasn't getting called at all. This Q&A helped me! – S Raghav Aug 04 '22 at 19:10