【发布时间】:2012-01-09 04:04:18
【问题描述】:
我正在尝试创建一个 JAR,我可以将它放在非开发机器上并运行一些运行 Web 服务的单元测试。我读过在 Maven 中有很多方法可以做到这一点。我专注于使用 test-jar 目标,因为这似乎是最合乎逻辑的方法。
我的测试在一个单独的 maven 项目中,其文件夹结构为:
/myProject/src/test/java/org/testplatform/services/test/<name of testng test>.java
这是我在 pom 中的 test-jar 的配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org/testplatform/services/test/MainTestClass.java</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
当我执行生成的 JAR 时出现此错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/ITestListener
Caused by: java.lang.ClassNotFoundException: org.testng.ITestListener
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
有人可以帮助我了解我需要在我的 pom 和/或测试中进行哪些更改以创建一个独立的、可执行的 JAR 来运行 testng 单元测试吗?一个演示项目会很有帮助。
【问题讨论】:
标签: unit-testing maven jar testng executable-jar