Skip to content

Conversation

@matthiasblaesing
Copy link
Contributor

JUnit allows to declare tests on nested (NestedClass2 and DoubleNestedClass3 below) and also non public top level classes (PackagedTopLevelNonPublicTest) in addition to the traditional toplevel class tests (test4):

public class PackagedTopLevelTest {
  @Test
  public void test4() {
    System.out.println("Executed test.PackagedTopLevel#test4");
  }

  @Nested
  class NestedClass2 {
    @Test
    public void test6() {
      System.out.println("Executed test.PackagedTopLevelTest$NestedClass2#test6");
    }
    @Nested
    class DoubleNestedClass3 {
      @Test
      public void test8() {
        System.out.println("Executed test.PackagedTopLevelTest$NestedClass2$DoubleNestedClass3#test8");
      }
    }
  }

}

class PackagedTopLevelNonPublicTest {
    @Test
    public void test10() {
        System.out.println("Executed test.PackagedTopLevelNonPublic#test10");
    }
}

In addition to that Unittests can be placed in the default package and support the same set of special cases.

This PR adds support for these cases:

  • running of single test from nested/top level non-public classes
  • rerunning/debugging test from nested/top level non-public classes

In addition while investigating the fix, it was observed, that the unittest result extraction sometimes yielded an incomplete set. I.e. some of the test data was missing. Analysis showed, that he XML reports from maven surefire were not yet written/updated while the console output was already done.

Another identified problem fixed here was incomplete detection of Jupiter (JUnit5) usage in the unittest run.

It is inspired by the work from @ratcashdev.

Closes: #3975
Closes. #3995

@matthiasblaesing matthiasblaesing added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) Maven [ci] enable "build tools" tests labels Jul 21, 2025
@matthiasblaesing matthiasblaesing marked this pull request as draft July 21, 2025 19:49
@matthiasblaesing matthiasblaesing added the Gradle [ci] enable "build tools" tests label Jul 22, 2025
@matthiasblaesing matthiasblaesing changed the title Maven-JUnit Integration: Support nested and toplevel non-public tests and stabiize result extraction Maven/Gradle-JUnit Integration: Support nested and toplevel non-public tests and stabiize result extraction Jul 22, 2025
@matthiasblaesing matthiasblaesing added the ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) label Jul 22, 2025
@apache apache locked and limited conversation to collaborators Jul 22, 2025
@apache apache unlocked this conversation Jul 22, 2025
@matthiasblaesing matthiasblaesing marked this pull request as ready for review July 23, 2025 18:28
The assumption in the method is, that the names of nested classes are
placed into an index, but that is not the case. In the index only
toplevel classes are recorded. So this:

```
package test;

public class Demo {
static class Nested {}
}

class NonPublic {}
```

will yield:

```
test.NonPublic
test.Demo
```

But the nested class `test.Demo.Nested` will not be found! So use the
index only as a validator if a NestedClass is provided.

javac.compilerargs=-Xlint -Xlint:-serial
javac.source=1.8
javac.target=17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

javac.release=17

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - overlooked that. I would have sworn I had that corrected before, oh well 😄

DefaultReplaceTokenProvider looks up NestedClass from invocation
context to determine the classname used in tokens. For unittests the
NestedClass is passed as part of the SingleMethod so extract it from
there.
It was observed, that also for singlethreaded runs the XML reports are
sometimes created delayed. The existing retry logic for parallel runs
needs to be enhanced to cover the single threaded case and also more
than one retry.

Observed with a trivial nested testcases at least one retry was
observed.
…e to indicate JUnit5 usage

This pom.xml runs fine with surefire, but triggers the update
surefire/junit warning:

```
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>TestNested</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>17</maven.compiler.release>
        <exec.mainClass>test.testnested.TestNested</exec.mainClass>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.13.3</version>
            <scope>test</scope>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.13.3</version>
            <scope>test</scope>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
       <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.5.3</version>
            </plugin>
        </plugins>
    </build>
</project>
```
@matthiasblaesing matthiasblaesing merged commit 59fb5e2 into apache:master Aug 26, 2025
75 of 76 checks passed
@matthiasblaesing matthiasblaesing deleted the junit-nested branch October 5, 2025 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) Gradle [ci] enable "build tools" tests Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) Maven [ci] enable "build tools" tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Junit @Nested test classes

2 participants