-
Notifications
You must be signed in to change notification settings - Fork 1k
Open
Description
When I package it into a jar package and run it, it prompts that the configuration file is damaged and skips the test. When I run the test without using the jar package, the use cases run normally.
Java-version:17 TestNg-version:7.11.0
(base) PS D:\test\target> java -jar .\SV-1.0-SNAPSHOT.jar
SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation
SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
===============================================
All Test Suite
Total tests run: 2, Passes: 0, Failures: 0, Skips: 2
Configuration Failures: 4, Skips: 0
===============================================
This is the main class I run:
package com.Test;
import org.testng.TestNG;
import org.testng.xml.XmlSuite;
import org.testng.xml.internal.Parser;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
public class AutomationRunner {
public static void main(String[] args) {
try {
String resourcePath = "TestNgXml/testng.xml";
File tempXml = extractResourceToTemp(resourcePath);
Parser parser = new Parser(tempXml.getAbsolutePath());
List<XmlSuite> suites = parser.parseToList();
TestNG testNG = new TestNG();
testNG.setXmlSuites(suites);
testNG.setUseDefaultListeners(false);
testNG.run();
tempXml.deleteOnExit();
} catch (Exception e) {
e.printStackTrace();
}
}
private static File extractResourceToTemp(String resourcePath) throws IOException {
InputStream inputStream = AutomationRunner.class.getClassLoader().getResourceAsStream(resourcePath);
if (inputStream == null) {
throw new IOException("Resource not found: " + resourcePath);
}
File tempFile = File.createTempFile("testng-", ".xml");
Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
inputStream.close();
return tempFile;
}
}