Skip to content

Maven-JUnit Integration: Support nested and toplevel non-public tests and stabiize result extraction #8664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

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

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.
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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy