From 3f9fd44bfa31d9fdbb104cb09af17a33d5c9a8ae Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Thu, 10 Oct 2024 14:50:58 -0700 Subject: [PATCH] Add support for @AfterAll in XML report Writing a simple test like ``` package com.library; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; public class AlwaysFailTest { @Test public void doNothingTest() { System.out.println("Hi there!"); } @AfterAll public static void alwaysFail() { throw new RuntimeException("Always failing."); } } ``` Produces an XML that seems to look like it succeeds. ```xml ``` Bazel itself correctly identifies it fails. The Junit4 reporter also included with Bazel natively correclty reports the failure in the XML output. Augment the Junit listener to add support for static methods and add them to the JUnit output. Doing so produces the following XML. ```xml ``` --- .../junit5/BazelJUnitOutputListener.java | 9 ++++++--- .../contrib_rules_jvm/junit5/TestData.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/BazelJUnitOutputListener.java b/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/BazelJUnitOutputListener.java index f132c2eb..f792337b 100644 --- a/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/BazelJUnitOutputListener.java +++ b/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/BazelJUnitOutputListener.java @@ -108,8 +108,11 @@ private Map> matchTestCasesToSuites_locked( // In all cases we're looking for the "class" segment, pull the UniqueID and map that from the // results List segments = testCase.getId().getUniqueIdObject().getSegments(); - - if (segments.size() == 3 || segments.size() == 5) { + + if (segments.size() == 2) { + parent = results.get(testCase.getId().getUniqueIdObject()); + } + else if (segments.size() == 3 || segments.size() == 5) { // get class / test data up one level parent = testCase @@ -161,7 +164,7 @@ private List findTestCases_locked() { // are identified by the fact that they have no child test cases in the // test plan, or they are marked as tests. TestIdentifier id = result.getId(); - return id.isTest() || testPlan.getChildren(id).isEmpty(); + return id.getSource() != null || id.isTest() || testPlan.getChildren(id).isEmpty(); }) .collect(Collectors.toList()); } diff --git a/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/TestData.java b/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/TestData.java index 4feb54a6..7047e7b1 100644 --- a/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/TestData.java +++ b/java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/TestData.java @@ -146,4 +146,17 @@ public boolean isDynamic() { public Instant getStarted() { return this.started; } + + @Override + public String toString() { + return "TestData{" + + "id=" + id + + ", reportEntries=" + reportEntries + + ", started=" + started + + ", finished=" + finished + + ", reason='" + reason + '\'' + + ", result=" + result + + ", dynamic=" + dynamic + + '}'; + } }