我有一个只包含测试类的项目,这些测试类都是从一个抽象基类扩展而来的。测试在intellij中运行正常,并且在运行mvn clean install时使用指定的正确配置文件。
我想要做的是创建一个测试jar并从命令行运行测试,所以我使用这些命令:
mvn -DoutputDirectory=target -f pom.xml dependency:copy-dependencies
第一个命令是获取所有依赖项并将它们放在目标文件夹中创建的测试jar旁边。
java -cp .;target/* org.testng.TestNG -testjar target/my-test-jar.jar -xmlpathinjar sanity.xml
我明白了:
[TestNG] [ERROR]
Cannot instantiate class com.myPackage.MyTestClass
该类肯定存在,我可以从intellij内部运行sanity.xml。
最奇怪的是我可以从我的项目中删除java文件,然后把它放在:
public static void main(String[] args){
args = new String[4]
args[0] = "-testjar"
args[1] = "target/my-test-jar.jar"
args[2] = "-xmlpathinjar"
args[3] = "sanity.xml"
org.testng.TestNG.main(args)
}
在我项目的另一个Test类中,它将设法从jar文件中实例化MyTestClass。
有没有人对此有任何想法?我没有得到任何堆栈跟踪或日志文件,只是它无法实例化类的简单事实。我希望有人之前遇到过这个并且知道如何解决它。
干杯, 大卫
[编辑 - 添加部分pom文件]
<profiles>
<profile>
<id>buildOnly</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/groovy</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-resource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/test/resources</directory>
<targetPath>resources</targetPath>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.4</version>
<extensions>true</extensions>
<configuration>
<source>1.6</source>
<providerSelection>1.8</providerSelection>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.gmaven.runtime</groupId>
<artifactId>gmaven-runtime-1.8</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<providerSelection>1.8</providerSelection>
<sources>
<fileset>
<directory>${pom.basedir}/src/test/groovy</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</fileset>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>integrationTest</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/groovy</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-resource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/test/resources</directory>
<targetPath>resources</targetPath>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.4</version>
<extensions>true</extensions>
<configuration>
<source>1.6</source>
<providerSelection>1.8</providerSelection>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.gmaven.runtime</groupId>
<artifactId>gmaven-runtime-1.8</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<providerSelection>1.8</providerSelection>
<sources>
<fileset>
<directory>${pom.basedir}/src/test/groovy</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</fileset>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<suiteXmlFile>${testng.xml.file}</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.testng.reporters.TestHTMLReporter,org.testng.reporters.JUnitXMLReporter,org.testng.reporters.SuiteHTMLReporter,org.testng.reporters.FailedReporter,org.testng.reporters.EmailableReporter,org.testng.reporters.JUnitReportReporter</value>
</property>
</properties>
<systemProperties>
<property>
<name>isCI</name>
<value>${isCI}</value>
</property>
<property>
<name>browser</name>
<value>${browser}</value>
</property>
<property>
<name>remote</name>
<value>${remote}</value>
</property>
<property>
<name>port</name>
<value>${port}</value>
</property>
<property>
<name>environment</name>
<value>${environment}</value>
</property>
<property>
<name>subDomain</name>
<value>${subDomain}</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
答案 0 :(得分:0)
如果测试正确定位到src/test/java并且根据unit test naming conventions正确命名,那么测试只需从命令行通过
运行mvn test
但看起来你的测试名称不同会导致你的问题。
答案 1 :(得分:0)
消息显示“无法实例化”,而不是“无法找到”。
TestNG可以找到你的类但无法实例化它。在你的问题中提供的第一条信息将是MyTestClass
的来源,但即便没有,我猜你没有定义一个简单的无参数构造函数:
public MyTestClass() {}