Skip to content

Programically Loading TestNG Suite from JAR File Fails to Delete Temporary Copy of Suite File #2825

@speedythesnail

Description

@speedythesnail

TestNG Version

7.6.1

Expected behavior

When programmatically running TestNG, TestNG should load a suite yaml / xml file from a JAR.

Actual behavior

TestNG JarFileUtils copies the file from the jar to a temporary path, after it loads the suite file it attempts to delete the file.

If JarFileUtils fails to delete the file, it throws a FileNotFoundException, no matter what the cause is. This is not the case on my machine, the file definitely exists where it was reported to have not existed.

Is the issue reproducible on runner?

  • Shell
  • Maven
  • Gradle
  • Ant
  • Eclipse
  • IntelliJ
  • NetBeans
  • PowerShell (java -jar ${JAR_FILE} ), Adoptium JDK 17.0.4.101

Test case sample

Spring Configuration to set up TestNG

@Configuration
@Slf4j
public class TestNGConfig {
    @Bean
    public TestNG testNG() throws URISyntaxException, IOException {
        TestNG testNG = new TestNG();

        setTestNGSuiteFile(testNG);
        setTestNGThreads(testNG);
        setTestNGParallelMode(testNG);

        return testNG;
    }
    private void setTestNGSuiteFileInJar(TestNG testNG, final URI testNGXmlUri) {
        // Get the location of the JAR file that this class is executed from, so we can get the
        // YAML / XML testng suite file and programmatically run it.
       // e.g. C:\testApplication.jar or /usr/local/bin/TestApplication.jar
        final String jarFileLocation = getJarFileLocation(testNGXmlUri);

        testNG.setXmlPathInJar("BOOT-INF/classes/testng.yaml");
        testNG.setTestJar(jarFileLocation);
    }
    // Other non-important methods exists but are redacted for brevity
    ......
}

Main TestNG run:

@Service
public class TestService {
    final TestNG testNG;

    public TestService(final TestNG testNG) {
        this.testNG = testNG;

        testNG.run();
    }
}

Resultant Exception:

 Caused by: java.io.FileNotFoundException: Failed to delete file: C:\Users\steve\AppData\Local\Temp\testngXmlPathInJar-17673113249698129403\BOOT-INF\classes\testng.yaml
        at org.testng.JarFileUtils.delete(JarFileUtils.java:112) ~[testng-7.6.1.jar!/:7.6.1]
        at org.testng.JarFileUtils.delete(JarFileUtils.java:110) ~[testng-7.6.1.jar!/:7.6.1]
        at org.testng.JarFileUtils.delete(JarFileUtils.java:110) ~[testng-7.6.1.jar!/:7.6.1]
        at org.testng.JarFileUtils.delete(JarFileUtils.java:110) ~[testng-7.6.1.jar!/:7.6.1]
        at org.testng.JarFileUtils.testngXmlExistsInJar(JarFileUtils.java:92) ~[testng-7.6.1.jar!/:7.6.1]
        at org.testng.JarFileUtils.extractSuitesFrom(JarFileUtils.java:52) ~[testng-7.6.1.jar!/:7.6.1]
        ... 38 common frames omitted

The file does exist though:

 PS> Get-Item -Path C:\Users\steve\AppData\Local\Temp\testngXmlPathInJar-17673113249698129403\BOOT-INF\classes\testng.yaml

Directory: C:\Users\steve\AppData\Local\Temp\testngXmlPathInJar-17673113249698129403\BOOT-INF\classes

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
------         11/5/2022   9:23 PM            126 testng.yaml

I am currently opening a PR for this issue, this issue is submitted so I have a Github # to add to the CHANGES.txt file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions