Java JUnit 5 annotations differences

Nikolas :

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.

glytching :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=467916&siteId=1