Why don't JUnit 5 tests inherit @Test annotation from abstract classes?

I just realized (when porting legacy code from JUnit 4 to JUnit 5) that some of our test methods fail because they don’t have @Test annotation. They do not have this because they redefine methods from an abstract superclass (where the annotation is present).

I can easily fix this by adding @Test to each method. But I was wondering if this is intentional behavior. It has changed from JUnit 4 to 5, but I cannot find anything in the official JUnit5 user guide or anywhere else.

In accordance with this question, annotations are usually not inherited. But it looks like this was intentionally changed in the new version of JUnit. (Or am I missing something?)

Abstract test class

 import org.junit.jupiter.api.Test; abstract class AbstractJUnit5Test { @Test void generalTest() { System.out.println("This is a test in the abstract class"); } @Test abstract void concreteTest(); } 

Specific test class

 import org.junit.jupiter.api.Test; class ConcreteJUnt5Test extends AbstractJUnit5Test { // only gets executed with an additional @Test annotation: @Override void concreteTest() { System.out.println("This is from the concrete test method."); } } 
+5
source share
2 answers

This is an unintended difference between JUnit 4 and JUnit Jupiter.

See https://github.com/junit-team/junit5/issues/960 for details

Edit: after further research, it seems that this (convenient) behavior from JUnit 4 is actually unintentional. See Sam's last comment at https://github.com/junit-team/junit5/issues/960#issuecomment-316114648

+6
source

I suppose you are trying to create some kind of connection between your testing methods. Try it if you can use @Nested .
Here you can find a sample.

0
source

Source: https://habr.com/ru/post/1269930/


All Articles