I see there was introduced the new JUnit Jupiter according to JUnit 5 User Guide.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
I am confused about the same-named annotations I use across the library. Is there any significant difference between these two ones?
org.junit.Test
org.junit.jupiter.api.Test
The description from the linked page above explains the annotation org.junit.jupiter.api.Test
as following:
Denotes that a method is a test method. Unlike JUnit 4’s @Test annotation, this annotation does not declare any attributes, since test extensions in JUnit Jupiter operate based on their own dedicated annotations. Such methods are inherited unless they are overridden.
As far as I understand, the main difference is the new annotation attributes are replaced with the dedicated annotations and methods (ex. assertTimeout(...)
) unlike the old @Test(timeout = 1000)
.
The documentation speaks about the old annotation org.junit.Test
from the JUnit 4, however, doesn't clearly explain the purpose of the same annotation in the version JUnit 5, which is for my surprise not marked as @Deprecated
- it means there is still a purpose of using this annotation within JUnit 5, am I right?
My question is what is the purpose of org.junit.Test
in JUnit 5, why it's not deprecated and what my choice should be based on between the two annotations mentioned above.
Only one test runner can execute tests at a time in JUnit 4 (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously. JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of the Java 8 features.
Finally, since JUnit 5 provides backward compatibility, you can still run JUnit 4 tests with JUnit 5.
JUnit 5 is organized into multiple libraries, so only the features you need are imported into your project. With build systems like Maven and Gradle, including the right libraries is easy. JUnit 5 can use more than one extension at a time, which JUnit 4 could not (only one runner could be used at a time).
org.junit.Test
is not in JUnit5, that class is being provided by your dependency on JUnit Vintage.
JUnit Vintage includes the JUnit Vintage test engine and classes such as org.junit.Test
, this allows you to run JUnit4 tests alongside JUnit5 tests. It is a back compatability measure.
If you want to use only JUnit5 constructs (and leave JUnit4 out of your project) then just drop the dependency on JUnit Vintage and focus on org.junit.jupiter.api.Test
.
If you need to run JUnit4 and JUnit5 tests side by side (perhaps for the duration of a migraiton / cut-over period) then retain the dependency on JUnit Vintage but write all new test cases using org.junit.jupiter.api.Test
.
Update: in response to this ...
I still don't understand why
org.junit.Test
isn't deprecated.
It simply does not exist in JUnit Jupiter (so deprecation is moot) and it is not deprecated in JUnit Vintage because it is a core element of JUnit Vintage. However, I think I can see where you are coming from; you upgraded to JUnit5 and - confusingly - org.junit.Test
remains available on your classpath without @Deprecated
so there is no clear indication of its 'do not use' status. Perhaps you should think about whether you want/need any JUnit4 constructs supported when running JUnit5. If you do not need that then just do not include a dependency on JUnit Vintage and it'll be like org.junit.Test
never existed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With