Class JUnitXmlParser
Extracts test failure and error details from JUnit XML files generated by Maven Surefire,
Gradle, or similar test runners. Groups results by class name and supports display names
from system-out elements.
Ordering: Results are returned in a LinkedHashMap with classes ordered
to match typical JUnit console output: last-executed tests appear first. This makes
--index=1 refer to the most recently run failing test class, which aligns with
what developers see when scrolling test output.
This parser is intended for local, trusted XML files. Basic XXE protection is enabled, but full hardening is unnecessary for typical build tool output.
- Since:
- 1.0
- Author:
- Erik C. Thauvin
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic TestFailurecreateTestFailure(Element testCaseElement, Element failureElement, String defaultType) Creates aTestFailurefrom a test case and failure element.static StringextractDisplayName(Element testCaseElement) Extracts the JUnit display name from a test case'ssystem-outelement.static Map<String,TestClassFailures> extractTestFailuresGrouped(String xmlFilePath) Parses a JUnit XML report and groups test failures by class name.static StringgetAttributeOrDefault(Element element, String attributeName, String defaultValue) Returns an attribute value from an element, or a default if blank or missing.static StringgetTextContentTrimmed(Element element) Returns the trimmed text content of an element.static Map<String,TestClassFailures> parseTestCases(NodeList testCases) Parses test case nodes and groups failures by class name.static doubleParses a time value from a string.static voidvalidateFile(Path path) Validates that a file exists and is readable.
-
Field Details
-
MESSAGE_ATTR
XML attribute name for failure messages.- See Also:
-
NAME_ATTR
XML attribute name for test case names.- See Also:
-
TESTCASE_TAG
XML tag name for test case elements.- See Also:
-
-
Method Details
-
createTestFailure
public static TestFailure createTestFailure(Element testCaseElement, Element failureElement, String defaultType) Creates aTestFailurefrom a test case and failure element.Extracts test name, class name, execution time, display name, failure type, message, and stack trace. Uses the provided default type if the failure element lacks a type attribute.
- Parameters:
testCaseElement- the<testcase>elementfailureElement- the<failure>or<error>elementdefaultType- the failure type to use if not specified in the element- Returns:
- a populated
TestFailureinstance
-
extractDisplayName
Extracts the JUnit display name from a test case'ssystem-outelement.Searches for
display-name: <name>patterns in the system-out text content. Returns an empty string if no display name is found.- Parameters:
testCaseElement- the<testcase>element to search within- Returns:
- the display name if present, otherwise an empty string
-
extractTestFailuresGrouped
Parses a JUnit XML report and groups test failures by class name.Reads the specified file, extracts all
<testcase>elements containing<failure>or<error>children, and groups them by theclassnameattribute.Classes are returned in reverse XML order so that
--index=1refers to the last failing test class in the file. This matches the order developers see in console output, where the most recent failures appear at the bottom.- Parameters:
xmlFilePath- the path to the JUnit XML file- Returns:
- a map of class names to
TestClassFailuresordered by last-executed first - Throws:
NullPointerException- ifxmlFilePathis nullJUnitXmlParserException- if the file does not exist, is not readable, or parsing fails
-
getAttributeOrDefault
public static String getAttributeOrDefault(Element element, String attributeName, String defaultValue) Returns an attribute value from an element, or a default if blank or missing.- Parameters:
element- the XML elementattributeName- the attribute name to retrievedefaultValue- the value to return if the attribute is blank- Returns:
- the attribute value, or
defaultValueif blank
-
getTextContentTrimmed
Returns the trimmed text content of an element.- Parameters:
element- the XML element- Returns:
- the trimmed text content, or an empty string if null
-
parseTestCases
Parses test case nodes and groups failures by class name.Iterates through all
<testcase>elements, extracts failures and errors, and groups them intoTestClassFailuresinstances. The resulting map is reversed so classes appear in reverse XML order, matching console output where the last executed tests appear last.- Parameters:
testCases- aNodeListof<testcase>elements- Returns:
- a map of class names to grouped failures ordered by last-executed first
-
parseTime
Parses a time value from a string.Converts the input to a double. Returns
0.0if the input is null, empty, invalid, or not finite (NaN, Infinity).- Parameters:
timeStr- the time value as a string- Returns:
- the parsed time as a double, or
0.0if invalid
-
validateFile
Validates that a file exists and is readable.- Parameters:
path- the file path to validate- Throws:
JUnitXmlParserException- if the file does not exist, is not readable, or cannot be accessed
-