-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
JUnit supports public, protected and package-private test methods.
JUnit supports test class inheritance.
Public and protected methods can be overridden in JLS sense. JUnit recognizes this situation and runs the test only once. In test source code this is indicated with @Override (in any reasonably managed project).
Package-private methods do not override each other in JLS sense. Both parent and child classes can declare @Test void testFoo method without any warning and without need for @Override. It is expected that such test methods are considered unrelated by JUnit and both are run. Observed behavior is different: only the parent class test is run. The child test is silently ignored.
Using JUnit 6.0.0
Steps to reproduce
package a;
public abstract class TestParent {
@Test
void testParent()
{
System.out.println("TestParent.testParent");
}
@Test
public void testPublicNameCollision() {
System.out.println("TestParent.testPublicNameCollision");
}
@Test
void testPackagePrivateNameCollision() {
System.out.println("TestParent.testPackagePrivateNameCollision");
}
}package b;
public class TestChild extends a.TestParent {
@Test
void testChild() {
System.out.println("TestChild.testChild");
}
@Test
@Override // clear source-level indication of test overriding. As expected, only the child test code is run.
public void testPublicNameCollision() {
System.out.println("TestChild.testPublicNameCollision");
}
@Test
// no source-level indication of test overriding. This test code does not run, i.e. is silently skipped.
void testPackagePrivateNameCollision() {
System.out.println("TestChild.testPackagePrivateNameCollision");
}
}maven setup
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>6.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>mvn test prints
TestParent.testPackagePrivateNameCollision
TestParent.testParent
TestChild.testChild
TestChild.testPublicNameCollision
Note that there is no TestChild.testPackagePrivateNameCollision in the output.
Context
- Used versions (Jupiter/Vintage/Platform): junit-bom
6.0.0