繁体   English   中英

通过指向外部jar以编程方式运行testNG会引发ClassNotFoundException

[英]Running testNG programmatically by pointing to an external jar throws ClassNotFoundException

我想通过指向包含测试类的jar来以编程方式运行TestNG测试。 为此,首先解析testng.xml并获取类。 然后,使用URLCLassLoader将每个类加载到类路径中。 但这会引发org.testng.TestNGException: Cannot find class in classpath:异常中org.testng.TestNGException: Cannot find class in classpath:

下面是我尝试的代码

public void execute() {
    String testNGXmlPath = "/path/to/testng.xml";
    try {
        getClassesToLoad(testNGXmlPath);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }
    TestNG testng = new TestNG();
    List<String> suites = Lists.newArrayList();
    suites.add(testNGXmlPath);
    testng.setTestSuites(suites);
    testng.run();
}

public void getClassesToLoad(String path) throws ParserConfigurationException, IOException, SAXException {
    File inputFile = new File(path);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(inputFile);
    doc.getDocumentElement().normalize();
    NodeList nodeList = doc.getElementsByTagName("class");
    Element element;
    Node node;
    NamedNodeMap namedNodeMap;

    int i, length;
    length = nodeList.getLength();
    for (int j=0; j < length; j++) {
        element = (Element)nodeList.item(j);
        namedNodeMap = element.getAttributes();
        if (namedNodeMap != null) {
            for (i=0; i<namedNodeMap.getLength(); i++) {
                node = namedNodeMap.item(i);
                if (node.getNodeName() == "name") {
                    try {
                        loadClass("/path/to/testng.sample-1.0-SNAPSHOT.jar", node.getNodeValue());
                    } catch (ClassNotFoundException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    }
}

public static void loadClass(String jarFilePath, String className) throws MalformedURLException,
        ClassNotFoundException {
    File jarFile  = new File(jarFilePath);
    if (jarFile.isFile()) {
        URL url = jarFile.toURL();
        URL[] urls = new URL[] { url };
        ClassLoader cl = new URLClassLoader(urls);
        cl.loadClass(className);
    }
}

您正在使用URLClassLoader加载类加载器,但是要使您的类加载器加载的类对当前ContextualClassLoader可见,您需要对其进行设置。

请尝试通过Thread.currentThread().setContextClassLoader(); loadClass()方法中,然后尝试运行测试。 这样可以确保当TestNG启动时,它将使用您注入到当前线程中的ContextClassLoader来加载类,从而帮助您克服ClassNotFoundException

如果您打算从jar运行测试,则可以简单地打包它们并使用xml运行。
请参阅testng 文档的命令行部分。

如果您是Maven项目,可以参考以下步骤

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM