【问题标题】:Maven doesn´t check JUnit testsMaven 不检查 JUnit 测试
【发布时间】:2015-06-29 22:53:54
【问题描述】:

我主要使用本教程使用 Junit 配置 Maven: http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

我有一个测试类,当我执行“mvn test”时,这个测试类被 Maven 捕获但 Maven 不检查它(结果应该是 FAIL)。

C:\Users\User\git\example\example>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building passport 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ passport -
--
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ passport ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pa
ssport ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ passpor
t ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ passport ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.472 s
[INFO] Finished at: 2015-04-22T12:07:27+02:00
[INFO] Final Memory: 9M/155M
[INFO] ------------------------------------------------------------------------

我的测试类在 src/main/resources 中(控制台显示“正在复制 1 个资源”):

package main.resources;

import static org.junit.Assert.*;
import org.junit.Test;

public class exampleTest {

    @Test
    public void test() {

        int result = 5;
        assertTrue(result == 2);    
    }
}

我的 POM.xml:

<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>
    <groupId>example</groupId>
    <artifactId>example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>passport</name>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <parallel>methods</parallel>
                    <threadCount>10</threadCount>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.18.1</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>

    </build>
    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Maven 版本:

Apache Maven 3.3.1 (cab6659f9874fa96462afef40fcf6bc033d58c1c; 2015-03-13T21:10:2
7+01:00)
Maven home: C:\apache-maven-3.3.1\bin\..
Java version: 1.8.0_40, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_40\jre
Default locale: es_ES, platform encoding: Cp1252
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "dos"

知道为什么不检查测试用例吗?

其他问题,我不知道,因为当我使用 UTF-8 配置我的项目时会显示有关编码 CP1252 的警告。

【问题讨论】:

    标签: eclipse maven jakarta-ee junit maven-surefire-plugin


    【解决方案1】:

    您的测试类必须驻留在src/test/java 中,NOT 驻留在src/main/resources 中,看看default folder layout

    【讨论】:

    • 谢谢,这就是解决方案,我保留了默认文件夹布局的链接。我将此帖子标记为“已解决”,因为主要问题已解决,但我仍然对编码 cp1252 有疑问。
    • 查看我的补充答案。
    【解决方案2】:

    根据您的问题following should be made part of your pom

    <project>
      ...
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      ...
    </project>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-31
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 2010-09-19
      • 2019-05-02
      • 1970-01-01
      • 2018-06-18
      相关资源
      最近更新 更多