2

当我尝试使用 maven 编译多个源目录时,出现包 org.testng.annotations 不存在错误。我正在运行 webdriver 测试,我所有的测试都在导入 import org.testng.annotations。我正在使用 IntelliJ 12,我的 src 目录看起来像这样 -

源代码

  -->main
     --> java
        --> package1
            --> file1.java
        --> package2
            --> file2.java
  -->test
     --> java
        --> package1
           --> file1.java
           --> file2.java
        --> package2
        --> package3
        --> package4
        --> package5
        --> package6

我在 pom.xml 中使用的构建插件看起来像这样 -

<?xml version="1.0" encoding="UTF-8"?>
project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
    <artifactId>core-xxxxx</artifactId>
    <groupId>core-xxxxx</groupId>
    <version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>tests</artifactId>
   <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>2.32.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
    <build>
       <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
 <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>

                        <sources>
                            <source>src/main/java</source>
                            <source>src/test/java</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
</plugins>
</build>
</project>

为什么我收到 org.testng.annotations not found 错误?

4

2 回答 2

1

The default layout for a maven project is the following:

src
   main
      java
   test
      java

within the src/main/java/ directory the package name for you productive java class source files should added. within the src/test/java/ the package name for the unit tests java class source files.

And you should NOT change the layout if you don't have really really good reasons to do so.

Furthermore redefining the defaults of Maven defaults (target, target/classes, target/test-classes etc.) is against the convention over configuration paradigm.

Be aware that you need to follow a naming convention for unit tests which means you unit tests should be named like the following:

<includes>
 <include>**/*Test*.java</include>
 <include>**/*Test.java</include>
 <include>**/*TestCase.java</include>
</includes>

But in your case I assume that we are talking about integration tests which means you need to use the following naming convention

<includes>
 <include>**/IT*.java</include>
 <include>**/*IT.java</include>
 <include>**/*ITCase.java</include>
</includes>

Apart from that you should now that unit tests in Maven will be executed by the maven-surefire-plugin whereas the integration tests will be executed by the maven-failsafe-plugin.

于 2013-05-16T05:22:55.287 回答
0

为了绕过 testng annotations not found 错误,我最终将基类从 src/main/java/package1/baseclass.java 移动到 src/main/test/package1/baseclass.java 并解决了 testng annotations 错误

于 2013-05-22T00:08:28.897 回答