Maven报告称已运行测试套件,但未运行任何JUnit测试。

5
我将尝试使用JUnit测试来测试Maven中的Java应用程序。
我有以下POM文件(仅相关部分):
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit4</artifactId>
                        <version>2.7.2</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>
  </plugin>

看起来正在运行“某些东西”。这是从“mvn -X test”输出的结果:

Running TestSuite
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.769 sec

Results :


Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

请注意"Running TestSuite"的提示。但是,它仍然无法找到我的测试方法。我怀疑这可能与这个问题有关:
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/apache/maven/surefire/surefire-testng/2.4.3/surefire-testng-2.4.3.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/apache/maven/maven-artifact/2.0/maven-artifact-2.0.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/codehaus/plexus/plexus-utils/1.0.4/plexus-utils-1.0.4.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/apache/maven/surefire/surefire-api/2.4.3/surefire-api-2.4.3.jar

它说正在使用 Junit 的版本 3.8.1...

然而,它继续说:

测试类路径:

 [DEBUG]   /home/julien/.m2/repository/junit/junit/4.8.2/junit-4.8.2.jar

我想知道“surefire test classpath”和“Test classpath”的区别是什么?
以下是我的测试方法名称和注释:
 @Test
    public void test() {

它位于这里:

src/test/java/com/cheetah/web/pages

有什么想法我做错了吗?以下是有效的POM:
<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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>cheetah</artifactId>
    <groupId>com.cheetah</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.cheetah</groupId>
  <artifactId>cheetah-web</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>cheetah-web</name>
  <build>
    <sourceDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/test/java</testSourceDirectory>
    <outputDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target/classes</outputDirectory>
    <testOutputDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target/test-classes</testOutputDirectory>
    <resources>
      <resource>
        <mergeId>resource-2</mergeId>
        <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/main/java</directory>
        <includes>
          <include>**/*.tml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <resource>
        <mergeId>resource-3</mergeId>
        <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <mergeId>resource-1</mergeId>
        <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/test/resources</directory>
      </testResource>
    </testResources>
    <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target</directory>
    <finalName>cheetah-web-1.0-SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-4</version>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>2.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.4</version>
        </plugin>
        <plugin>
          <artifactId>maven-ear-plugin</artifactId>
          <version>2.3.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-ejb-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-javadoc-plugin</artifactId>
          <version>2.5</version>
        </plugin>
        <plugin>
          <artifactId>maven-plugin-plugin</artifactId>
          <version>2.5</version>
        </plugin>
        <plugin>
          <artifactId>maven-rar-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0-beta-8</version>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>2.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-source-plugin</artifactId>
          <version>2.0.4</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.4.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.1-beta-1</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1-beta-1</version>
      </plugin>
      <plugin>
        <artifactId>maven-help-plugin</artifactId>
        <version>2.1.1</version>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <id>java.net</id>
      <url>http://download.java.net/maven/1</url>
      <layout>legacy</layout>
    </repository>
    <repository>
      <id>maven2-repository.dev.java.net</id>
      <name>Java.net Repository for Maven</name>
      <url>http://download.java.net/maven/2</url>
    </repository>
    <repository>
      <id>jboss</id>
      <name>jboss</name>
      <url>http://repository.jboss.org/maven2/</url>
    </repository>
    <repository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <updatePolicy>never</updatePolicy>
      </snapshots>
      <id>jboss-public-repository-group</id>
      <name>JBoss Public Maven Repository Group</name>
      <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </repository>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <url>http://repo1.maven.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <updatePolicy>never</updatePolicy>
      </snapshots>
      <id>jboss-public-repository-group</id>
      <name>JBoss Public Maven Repository Group</name>
      <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </pluginRepository>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Plugin Repository</name>
      <url>http://repo1.maven.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <dependencies>
    <dependency>
      <groupId>com.cheetah</groupId>
      <artifactId>cheetah-domain</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>com.cheetah</groupId>
      <artifactId>cheetah-service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-core</artifactId>
      <version>5.2.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-beanvalidator</artifactId>
      <version>5.2.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-spring</artifactId>
      <version>5.2.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-test</artifactId>
      <version>5.2.6</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>3.0.5.RELEASE</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.easymock</groupId>
      <artifactId>easymock</artifactId>
      <version>2.5.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-web-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <reporting>
    <outputDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target/site</outputDirectory>
  </reporting>
</project>

你的pom文件的构件类型是什么? - crowne
@crowne 父项目使用<packaging>pom</packaging>打包,包含测试的项目使用<packaging>war</packaging>打包。 - balteo
你可以尝试将Java源代码和测试移动到一个Java模块中,并进行jar打包,然后查看测试运行情况。然后可能会保留该结构,并将Java模块作为war项目的依赖项添加进去。 - crowne
另外,我怀疑由于战争结构,Java测试源的默认位置可能会被覆盖。您可以在pom文件中找到可以设置的系统属性来指定它。 - crowne
@Crowne,请注意它确实运行了“测试套件”(参见:运行TestSuite)。你真的认为它无法找到测试源吗? - balteo
1
这是您的答案...https://dev59.com/ZXI-5IYBdhLWcg3wFkOO - crowne
3个回答

3

在Maven中运行junit测试需要进行最小的设置。将junit添加到您的依赖项中:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.7</version>
    <scope>test</scope>
</dependency>

在 src/test/java/com/cheetah/web/pages 中创建一个类:
public class FooTest {
    @Test
    public void writingToStdOut_succeeds() throws Exception {
        System.out.println("I work!");
    }
}

运行mvn test即可,无需在surefire插件中进行任何配置。对于war项目和jar项目来说,这并没有什么不同。如果你按照以上两个步骤操作后仍然无法运行测试,则说明其他地方的配置出了问题,我们可能需要查看整个pom文件或者最好是effective pom来确定问题所在。
请注意,Surefire只会在同一构建中运行testng测试或junit测试,而不会同时运行两种测试。

1
Ryan,谢谢。我已经删除了surefire配置,但仍然完全无法工作。我已经生成了有效的POM文件,但仍然看不出问题在哪里... - balteo
4
除了刚才我回顾了一下你的帖子并发现testng被列在了你的类路径中。你需要将其移除。最近我检查过,Surefire只会运行要么 testng测试 要么 junit测试,而不是在同一个构建中同时运行两者。除非你有其他的集成方法,否则你需要选择其中一个。 - Ryan Stewart
嗨@RyanStewart,我遇到了无法工作的情况。我该如何在这里发布整个POM?我正在处理一个相当大的项目,其中有许多模块。我专门在处理单个模块,那么您需要哪个POM才能让您看到?非常感谢。 - Aerox
问题在于错误的导入。我使用了 org.junit.Test 而不是 org.testng.annotations.Test,因此我一直收到错误。无论如何,谢谢;-) - Aerox
有人能帮忙解决这个问题吗?https://dev59.com/Uajka4cB1Zd3GeqPEtIq - Rohit Barnwal
显示剩余2条评论

1

只是提一下大家都已经知道的事情,但这仍然会偶尔让我感到困惑。请记住,测试将被“看到”的命名约定,如果您没有正确命名测试类,则会被忽略。

运行中

mvn -X test

展示给我们(精简版):
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test' with basic configurator -->
[DEBUG]   (s) additionalClasspathElements = []
...
[DEBUG]   (s) excludes = [**/Abstract*.java]
[DEBUG]   (s) includes = [**/*Tests.java, **/*Test.java]

因此,默认情况下,只有以"Test.java"或"Tests.java"结尾的类才会被捕获。


0

我的问题是由于maven版本依赖出了问题。我手动添加时犯了一个错误。

建议:

您应该始终通过“插入依赖项”菜单添加依赖项。当您的光标位于xml文件中的<dependency></dependency>之间时,可以使用快捷键“crtl”+“space”显示它。在“选择依赖项”菜单中,输入“junit”并选择您的junit版本。单击“确定”后,将自动添加您的依赖项。这有助于避免手动输入不正确版本的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接