Skip to content

Commit 786369d

Browse files
fix: support nested classes properly
Previously if you had deeply nested classes the BazelJUnitOutputListener would throw an exception. In JUnit5 TestExecutionListeners are not expected to throw exceptions and any exception just gets swallowed and logged as a warning. This would cause cases where an exception was thrown to output no test results and result in a successful test run even though there was a failure in the listener itself. The new logic attempts to get the closest parent segment that is a class type. This doesn't match the way things like gradle will output test XML but solves the problem with nested classes while keeping the output similar. In terms of output this actually fixes an issue that is visible in the NestedClassesTest today, where the Second nested classes function result gets grouped into the outer class while the static First classes function result appears in a separate group as shown below: ``` NestedClassesTest shouldBeExecuted shouldBeExecuted NestedClassesTest$First shouldBeExecuted ``` With the changes the grouping is fixed as shown below: ``` NestedClassesTest shouldBeExecuted NestedClassesTest$First shouldBeExecuted NestedClassesTest$Second shouldBeExecuted NestedClassesTest$Second$Third shouldBeExecuted NestedClassesTest$Second$Third$Fourth shouldBeExecuted NestedClassesTest$Second$Third$Fourth$Fifth shouldBeExecuted ``` These groups appear unordered in UI like IntelliJ which isn't ideal, but these changes closely align with how things are done today, while avoiding just silently passing with no test output if you take this version of the NestedClassesTest and run it using the previous BazelJUnitOutputListener code.
1 parent 6f2ddcb commit 786369d

14 files changed

+305
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite name="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$First" tests="1" skipped="0" failures="0" errors="0" timestamp="2025-07-23T17:23:19" hostname="MacBook-Pro.local" time="0.0">
3+
<properties/>
4+
<testcase name="shouldBeExecuted()" classname="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$First" time="0.0"/>
5+
<system-out><![CDATA[>>>> Executed test in NestedClassesTest$First
6+
]]></system-out>
7+
<system-err><![CDATA[]]></system-err>
8+
</testsuite>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite name="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third$Fourth$Fifth" tests="1" skipped="0" failures="0" errors="0" timestamp="2025-07-23T17:23:19" hostname="MacBook-Pro.local" time="0.001">
3+
<properties/>
4+
<testcase name="shouldBeExecuted()" classname="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third$Fourth$Fifth" time="0.001"/>
5+
<system-out><![CDATA[>>>> Executed test in NestedClassesTest$Second$Third$Fourth$Fifth
6+
]]></system-out>
7+
<system-err><![CDATA[]]></system-err>
8+
</testsuite>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite name="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third$Fourth" tests="1" skipped="0" failures="0" errors="0" timestamp="2025-07-23T17:23:19" hostname="MacBook-Pro.local" time="0.0">
3+
<properties/>
4+
<testcase name="shouldBeExecuted()" classname="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third$Fourth" time="0.0"/>
5+
<system-out><![CDATA[>>>> Executed test in NestedClassesTest$Second$Third$Fourth
6+
]]></system-out>
7+
<system-err><![CDATA[]]></system-err>
8+
</testsuite>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite name="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third" tests="1" skipped="0" failures="0" errors="0" timestamp="2025-07-23T17:23:19" hostname="MacBook-Pro.local" time="0.0">
3+
<properties/>
4+
<testcase name="shouldBeExecuted()" classname="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third" time="0.0"/>
5+
<system-out><![CDATA[>>>> Executed test in NestedClassesTest$Second$Third
6+
]]></system-out>
7+
<system-err><![CDATA[]]></system-err>
8+
</testsuite>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite name="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second" tests="1" skipped="0" failures="0" errors="0" timestamp="2025-07-23T17:23:19" hostname="MacBook-Pro.local" time="0.0">
3+
<properties/>
4+
<testcase name="shouldBeExecuted()" classname="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second" time="0.0"/>
5+
<system-out><![CDATA[>>>> Executed test in NestedClassesTest$Second
6+
]]></system-out>
7+
<system-err><![CDATA[]]></system-err>
8+
</testsuite>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite name="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest" tests="1" skipped="0" failures="0" errors="0" timestamp="2025-07-23T17:23:19" hostname="MacBook-Pro.local" time="0.0">
3+
<properties/>
4+
<testcase name="shouldBeExecuted()" classname="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest" time="0.0"/>
5+
<system-out><![CDATA[>>>> Executed test in NestedClassesTest
6+
]]></system-out>
7+
<system-err><![CDATA[]]></system-err>
8+
</testsuite>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.github.bazel_contrib.contrib_rules_jvm.comparative;
2+
3+
import org.junit.jupiter.api.Nested;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class NestedClassesTest {
7+
static class First {
8+
@Test
9+
public void shouldBeExecuted() {
10+
System.out.println(">>>> Executed test in NestedClassesTest$First");
11+
}
12+
}
13+
14+
@Nested
15+
class Second {
16+
@Test
17+
public void shouldBeExecuted() {
18+
System.out.println(">>>> Executed test in NestedClassesTest$Second");
19+
}
20+
21+
@Nested
22+
class Third {
23+
@Test
24+
public void shouldBeExecuted() {
25+
System.out.println(">>>> Executed test in NestedClassesTest$Second$Third");
26+
}
27+
28+
@Nested
29+
class Fourth {
30+
@Test
31+
public void shouldBeExecuted() {
32+
System.out.println(">>>> Executed test in NestedClassesTest$Second$Third$Fourth");
33+
}
34+
35+
@Nested
36+
class Fifth {
37+
@Test
38+
public void shouldBeExecuted() {
39+
System.out.println(">>>> Executed test in NestedClassesTest$Second$Third$Fourth$Fifth");
40+
}
41+
}
42+
}
43+
}
44+
}
45+
46+
@Test
47+
public void shouldBeExecuted() {
48+
System.out.println(">>>> Executed test in NestedClassesTest");
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third$Fourth$Fifth" time="0.0" tests="5" errors="0" skipped="0" failures="0">
3+
<properties>
4+
<property name="java.specification.version" value="21"/>
5+
<property name="sun.jnu.encoding" value="UTF-8"/>
6+
<property name="java.vm.specification.version" value="21"/>
7+
<property name="sun.java.launcher" value="SUN_STANDARD"/>
8+
<property name="user.country" value="US"/>
9+
<property name="jdk.debug" value="release"/>
10+
<property name="sun.cpu.endian" value="little"/>
11+
<property name="user.language" value="en"/>
12+
<property name="java.specification.vendor" value="Oracle Corporation"/>
13+
<property name="java.version.date" value="2024-07-16"/>
14+
<property name="file.separator" value="/"/>
15+
<property name="java.vm.compressedOopsMode" value="Zero based"/>
16+
<property name="line.separator" value="&#10;"/>
17+
<property name="java.specification.name" value="Java Platform API Specification"/>
18+
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
19+
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
20+
<property name="path.separator" value=":"/>
21+
<property name="file.encoding" value="UTF-8"/>
22+
<property name="java.vm.name" value="OpenJDK 64-Bit Server VM"/>
23+
<property name="native.encoding" value="UTF-8"/>
24+
<property name="sun.io.unicode.encoding" value="UnicodeBig"/>
25+
</properties>
26+
<testcase name="shouldBeExecuted" classname="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest" time="0.0">
27+
<system-out><![CDATA[>>>> Executed test in NestedClassesTest
28+
]]></system-out>
29+
</testcase>
30+
<testcase name="shouldBeExecuted" classname="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second" time="0.0">
31+
<system-out><![CDATA[>>>> Executed test in NestedClassesTest$Second
32+
]]></system-out>
33+
</testcase>
34+
<testcase name="shouldBeExecuted" classname="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third" time="0.0">
35+
<system-out><![CDATA[>>>> Executed test in NestedClassesTest$Second$Third
36+
]]></system-out>
37+
</testcase>
38+
<testcase name="shouldBeExecuted" classname="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third$Fourth" time="0.0">
39+
<system-out><![CDATA[>>>> Executed test in NestedClassesTest$Second$Third$Fourth
40+
]]></system-out>
41+
</testcase>
42+
<testcase name="shouldBeExecuted" classname="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third$Fourth$Fifth" time="0.0">
43+
<system-out><![CDATA[>>>> Executed test in NestedClassesTest$Second$Third$Fourth$Fifth
44+
]]></system-out>
45+
</testcase>
46+
</testsuite>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third$Fourth" time="0.002" tests="0" errors="0" skipped="0" failures="0">
3+
<properties>
4+
<property name="java.specification.version" value="21"/>
5+
<property name="sun.jnu.encoding" value="UTF-8"/>
6+
<property name="java.vm.specification.version" value="21"/>
7+
<property name="sun.java.launcher" value="SUN_STANDARD"/>
8+
<property name="user.country" value="US"/>
9+
<property name="jdk.debug" value="release"/>
10+
<property name="sun.cpu.endian" value="little"/>
11+
<property name="user.language" value="en"/>
12+
<property name="java.specification.vendor" value="Oracle Corporation"/>
13+
<property name="java.version.date" value="2024-07-16"/>
14+
<property name="file.separator" value="/"/>
15+
<property name="java.vm.compressedOopsMode" value="Zero based"/>
16+
<property name="line.separator" value="&#10;"/>
17+
<property name="java.specification.name" value="Java Platform API Specification"/>
18+
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
19+
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
20+
<property name="path.separator" value=":"/>
21+
<property name="file.encoding" value="UTF-8"/>
22+
<property name="java.vm.name" value="OpenJDK 64-Bit Server VM"/>
23+
<property name="native.encoding" value="UTF-8"/>
24+
<property name="sun.io.unicode.encoding" value="UnicodeBig"/>
25+
</properties>
26+
</testsuite>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="com.github.bazel_contrib.contrib_rules_jvm.comparative.NestedClassesTest$Second$Third" time="0.003" tests="0" errors="0" skipped="0" failures="0">
3+
<properties>
4+
<property name="java.specification.version" value="21"/>
5+
<property name="sun.jnu.encoding" value="UTF-8"/>
6+
<property name="java.vm.specification.version" value="21"/>
7+
<property name="sun.java.launcher" value="SUN_STANDARD"/>
8+
<property name="user.country" value="US"/>
9+
<property name="jdk.debug" value="release"/>
10+
<property name="sun.cpu.endian" value="little"/>
11+
<property name="user.language" value="en"/>
12+
<property name="java.specification.vendor" value="Oracle Corporation"/>
13+
<property name="java.version.date" value="2024-07-16"/>
14+
<property name="file.separator" value="/"/>
15+
<property name="java.vm.compressedOopsMode" value="Zero based"/>
16+
<property name="line.separator" value="&#10;"/>
17+
<property name="java.specification.name" value="Java Platform API Specification"/>
18+
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
19+
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
20+
<property name="path.separator" value=":"/>
21+
<property name="file.encoding" value="UTF-8"/>
22+
<property name="java.vm.name" value="OpenJDK 64-Bit Server VM"/>
23+
<property name="native.encoding" value="UTF-8"/>
24+
<property name="sun.io.unicode.encoding" value="UnicodeBig"/>
25+
</properties>
26+
</testsuite>

0 commit comments

Comments
 (0)