When migrating from JUnit4 to JUnit5 I found a change in the behaviour of JUnit4 and JUnit5 and wanted to check if the change was a bug in JUnit4 or in JUnit5 and how to do it correctly.
Lets assume the following structure:
One base class
public class BaseTestClass {
@Before
public void setUp(){
System.out.println("Base Test Class");
}
}
Another class that inherits from this base class
public class InheritsFromBase extends BaseTestClass {
@Override
public void setUp() {
System.out.println("I inherit from base");
super.setUp();
}
}
And an actual test class
public class ActualTests extends InheritsFromBase {
@Test
public void myTest(){
Assert.assertTrue(true);
}
}
If I run myTest()
in JUnit 4 then the setUp()
method of both, BaseTestClass
and InheritsFromBase
is called.
After migrating this code to JUnit5, the setUp()
methods are not called anymore. I had to manually add the @BeforeEach
annotation on InheritsFromBase
.
Resulting in the following classes:
public class BaseTestClass {
@BeforeEach
public void setUp(){
System.out.println("Base Test Class");
}
}
public class InheritsFromBase extends BaseTestClass {
@Override
@BeforeEach
public void setUp() {
System.out.println("I inherit from base");
super.setUp();
}
}
public class ActualTests extends InheritsFromBase {
@Test
public void myTest(){
Assertions.assertTrue(true);
}
}
So my question: Was the behaviour in JUnit4 correct or in JUnit5?