Reputation: 1129
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.
Upvotes: 15
Views: 22725
Reputation: 1129
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.
Upvotes: 86
Reputation: 189
Try to change it to static
.
The @BeforeAll
method must be static
unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS)
.
Upvotes: 18
Reputation: 31177
If you are converting an individual test class from JUnit 4 to JUnit Jupiter, chances are that you ran the test class previously in Eclipse.
If that's the case, Eclipse already has a saved Run Configuration for that test class with the version of JUnit set to 4.
To tell Eclipse to run the same test class now as a JUnit Jupiter test class, you'll need to edit the run configuration for that test class. Select "Run Configurations..." from the "Run" menu. Then change the "Test runner" from "JUnit 4" to "JUnit 5. Then click the "Run" button.
FYI: this is also documented in the Eclipse JUnit 5 documentation. Search for the Note starting with "If you are using an Eclipse workspace where you were running".
Hope this helps!
Upvotes: 2