Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@BeforeAll not working in SOME files in Eclipse

Tags:

eclipse

junit5

I want to convert from JUnit 4 to 5 in Eclipse Oxygen 4.7.3a. I thought adding the Jupiter library would be sufficient: libraries, build path, etc. However, @BeforeAll, @AfterAll, @BeforeEach, and @AfterEach do not get executed, but the @Test methods do--but of course they fail without the proper setup.

Interestingly, I can create a file using the new Junit 5 Jupiter wizard, and that test file works. I copy and paste the JUnit 5 annotations from the new file to my existing file, and it still doesn't work. I am beginning to wonder if Eclipse has some configuration info somewhere behind the scenes of which I am unaware.

like image 768
Falsoon Avatar asked May 12 '18 21:05

Falsoon


2 Answers

I was using

import org.junit.Test;

instead of

import org.junit.jupiter.api.Test;

which triggered the JUnit 5 runner to think it was working with a JUnit 4 file. Easy solution, but hard to find since no errors messages were generated, and the file still ran. Also made more mystifying because "Organize Imports" added the JUnit 4 Test class, and not the jupiter Test class.

like image 65
Falsoon Avatar answered Oct 17 '22 06:10

Falsoon


Try to change it to static. The @BeforeAll method must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).

like image 14
Kang Song Avatar answered Oct 17 '22 04:10

Kang Song