【问题标题】:maven run junit testmaven运行junit测试
【发布时间】:2013-08-23 16:13:03
【问题描述】:

我在使用 maven 项目编译和运行 JUnit 测试时遇到问题。

在 pom.xml 中我添加了这些行来配置 surfire 插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
        <encoding>UTF-8</encoding>                    
        <skipTests>false</skipTests>                                 
        <includes>
            <include>**/*.java</include>
        </includes>
    </configuration>

    <executions>
        <execution>
            <id>unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <skip>false</skip>
                <includes>
                    <include>**/Client*.java</include>
                </includes>   
            </configuration>
        </execution>
    </executions>
</plugin>

测试类位于“src/test/java//”路径下。

当我运行命令 mvn test 时,我得到了这个输出:

[INFO] Storing buildScmBranch: trunk
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 10 resources
[INFO] [cxf-codegen:wsdl2java {execution: generate-sources}]
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Not copying test resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Not compiling test sources
[INFO] [surefire:test {execution: default-test}]
[INFO] Tests are skipped.
[INFO] [surefire:test {execution: unit-test}]
[INFO] No tests to run.
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)

我真的不知道我错在哪里。

编辑: 我将 pom 配置更改如下,因为我还注意到测试类也没有编译:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.2</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
    <executions>
        <execution>
            <id>default-testCompile</id>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <skip>false</skip>
                <additionalClasspathElements>
                    <additionalClasspathElement>${basedir}/target/*.jar</additionalClasspathElement>
                </additionalClasspathElements>
                <directory>${basedir}/src/test/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes> 
            </configuration>
        </execution>
    </executions>
</plugin>

这样 maven 开始考虑我的测试源并尝试编译它,即使它因为我使用 cxf-codegen-plugin 生成代码而无法编译它们,该测试编译似乎也看不到它。

【问题讨论】:

    标签: java maven


    【解决方案1】:

    可能是如herehere 所述的maven surefire 插件缺陷。

    【讨论】:

      【解决方案2】:

      错误表示没有测试用例匹配**/Client*.java

      确保模式正确。此外,mvn -X(启用调试输出)可能会给您一些提示,说明 Maven 为何找不到任何测试。

      【讨论】:

      • 所有测试类都在它们的包目录下。我必须指定它还是“**”就足够了?运行 mvn -X 并没有给我更多提示。
      • 测试类的名称应以“Test”结尾。默认模式为**/*Test.java。如果你想要所有文件,模式应该是**/*.java
      • 非常感谢,我用 */.java 替换了我的 */Client.java
      【解决方案3】:

      如果您遵循 Maven 默认结构(代码在 src/main/java 中,测试在 src/test/java 中),则不需要在 pom.xml 中进行任何特定配置(测试目标在 Maven 中构建)。

      尝试删除特定的surefire配置并运行:

      mvn test
      

      【讨论】:

      • 我添加了surefire配置,因为测试还是不行,希望能解决问题。
      • “测试不起作用”:错误消息是否相同(没有测试可运行)?你能发布你的项目结构吗? mvn test 应该够用了。
      • 错误信息是“测试被跳过”,因此我决定在 pom.xml 中配置 surfire。项目结构是标准的。
      猜你喜欢
      • 2021-12-16
      • 2020-12-31
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 2016-03-09
      • 2020-12-27
      • 2012-12-04
      相关资源
      最近更新 更多