1

我有一个项目,它只包含所有从一个抽象基类扩展的测试类。测试在 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)
}

在我项目的另一个测试类中,它将设法从 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>
4

2 回答 2

0

消息说“无法实例化”,而不是“找不到”。

TestNG 可以找到你的类但不能实例化它。在您的问题中提供的第一条信息将是 的来源MyTestClass,但即使没有,我猜您也没有定义一个简单的无参数构造函数:

public MyTestClass() {}
于 2012-11-15T17:05:46.910 回答
0

如果您的测试正确位于src/test/java并根据单元测试命名约定正确命名,那么您的测试可以简单地通过命令行运行

mvn test

但看起来你的测试命名不同,这会导致你的问题。

于 2012-11-15T14:18:16.030 回答