question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

StringIndexOutOfBoundsException in findClassesInPackage - Surefire/Maven - JDK 11 fails, JDK 8 succeeds.

See original GitHub issue

TestNG Version

7.1.0

Expected behavior

TestNG reads the package name correctly, succeeds in doing a substring

Actual behavior

TestNG keeps the package name from another class when doing a substring on current class, fails with error.

[ERROR] Caused by: java.lang.StringIndexOutOfBoundsException: begin 35, end 11, length 17
[ERROR]         at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
[ERROR]         at java.base/java.lang.String.substring(String.java:1874)
[ERROR]         at org.testng.internal.PackageUtils.findClassesInPackage(PackageUtils.java:112)
[ERROR]         at org.testng.xml.XmlPackage.initializeXmlClasses(XmlPackage.java:68)
[ERROR]         at org.testng.xml.XmlPackage.getXmlClasses(XmlPackage.java:59)

Having instrumented a copy of TestNG jar with print statements, I can see that this fails when a Jar has its files enumerated. Specifically jaxb-api-2.3.1.jar, which include a file ‘module-info.class’, which lacks a package. PackageUtils then tries to do a substring based on the wrong package, with a class name that has no package, and fails.

This does not occur during the JDK 8 build, as Java 8 doesn’t cause the JAXB jars to be referenced in this part of the build, but Java 11 does.

My simple print statement: org.testng.internal.PackageUtils#findClassesInPackage

for (URL url : dirs) {
  String protocol = url.getProtocol();
  if (!matchTestClasspath(url, packageDirName, recursive)) {
    continue;
  }
  System.err.println("url = " + url);

With Java 8:

url = file:/C:/projects/testng-failure-case/target/test-classes/
url = file:/C:/projects/testng-failure-case/target/classes/

With Java 11:

url = file:/C:/projects/testng-failure-case/target/test-classes/
url = file:/C:/projects/testng-failure-case/target/classes/
url = jar:file:/C:/Users/jsnell/.m2/repository/org/apache/logging/log4j/log4j-api/2.11.2/log4j-api-2.11.2.jar!/META-INF/versions/9/
url = jar:file:/C:/Users/jsnell/.m2/repository/org/apache/logging/log4j/log4j-core/2.11.2/log4j-core-2.11.2.jar!/META-INF/versions/9/
url = jar:file:/C:/Users/jsnell/.m2/repository/javax/xml/bind/jaxb-api/2.3.1/jaxb-api-2.3.1.jar!/META-INF/versions/9/

Is the issue reproductible on runner?

Completely reproducible with Maven test case using attached source. See output: testng.failure.output.txt

Test case sample

testng-failure-case.zip

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

1reaction
pilophaecommented, Sep 21, 2020

The module-info.class present in for example the JAXB-API JAR triggers this with Java 11. Only if a recursive package name is set in the TestNG suite (<package name=".*">). When debugging, it looks like the packageName variable is kept from the previous file when reading the JAR file, causing out if index when assuming that module-info.class is also in a package.

0reactions
john-snellcommented, Sep 21, 2020

build-helper-maven-plugin appears to resolve the issue. A co-worker came up with this variant of the test case, though it appears to be running the tests twice now. testng-failure-case-working.zip

Read more comments on GitHub >

github_iconTop Results From Across the Web

Maven Surefire tests failing after updating to Java JDK 11
I recently updated to use Java JDK 11.0.8 but when surefire tests are failing and i have no idea why. JUnit tests pass...
Read more >
Bountysource
StringIndexOutOfBoundsException in findClassesInPackage - Surefire/Maven - JDK 11 fails, JDK 8 succeeds.
Read more >
Java 8 builds suddenly started to fail, with no change on our side
We develop an application in Java 8, with spring boot 2.0.3.RELEASE and gitlab as the chosen git repository. We do our builds on...
Read more >
Maven Surefire Plugin – History
maven-surefire-report-plugin fails on JDK 11 ... The plugin supports Java 1.8 and Maven Plugin API 3.2.5. Many bug fixes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found