2012-11-15 19 views
1

我有一个只包含测试类的项目,这些测试类都从一个抽象基类扩展而来。这些测试在intellij中运行良好,并且在运行mvn clean install时指定了正确的配置文件。无法在测试罐内实例化类

我想要做的就是创建一个试验瓶并运行命令行测试,所以我使用这些命令:

mvn -DoutputDirectory=target -f pom.xml dependency:copy-dependencies 

这第一个命令是越来越所有的依赖,并把它们沿在目标文件夹中创建的测试罐旁边。

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) 
} 

内的另一个测试类我的项目,它将设法从罐子里实例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

你可以显示这个POM文件项目? – khmarbaise

+0

不是全部,我可以显示配置文件和插件部分。我将编辑我的文章并将其放入。 – user1147618

回答

0

如果测试正确定位到src/test/java和的基础上正确的unit test naming conventions比你的测试可以简单地命名在命令行通过

mvn test 

运行,但它看起来你的测试被命名为不同的,这将导致你的PR oblems。

+0

这些测试可以通过mvn test的命令行运行。问题是我想从一个由maven构建的测试jar中运行测试。 – user1147618

+0

为什么?你在谈论单元测试还是集成测试?此外,在看看你的poms之后,你的集成测试应该由maven-failsafe-plugin来代替maven-surefire-plugin来处理。不需要通过build-helper-plugin在src/test/resources中添加,因为它在Maven中是默认的。 skipTest非常糟糕。 – khmarbaise

+0

通过这种方式,我们可以在存储库中拥有一个jar文件,该文件夹可以在任何能够访问存储库的jenkins机器上运行,而不需要访问我们的源代码管理器或设置需要远程访问jenkins的工作人员机。 这些测试是集成测试,在创建jar时运行它们没有什么意义,尽管它们可以使用-P integrationTest配置文件。 感谢您指出不必要的src/test/resources行。 从jar中执行时我没有使用surefire插件(我不认为)。 – user1147618

0

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

TestNG可以找到你的类,但不能实例化它。的首部资料片在你的供应问题将是MyTestClass的来源,但即使没有,我猜你没有定义一个简单参数的构造函数:

public MyTestClass() {} 
相关问题