为什么mvn test运行测试不显示测试统计信息和测试结果

发布于 2022-09-04 04:39:50 字数 5952 浏览 15 评论 0

如题,为什么我用mvn test运行测试时不显示别人运行时显示的测试统计信息(一共多少个测试,测试通过了几个等等)。就是下面这样的统计信息:

-------------------------------------------------------  
T E S T S  
-------------------------------------------------------  
Running test.TestHello.TestHello  
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.151 sec  
  
Results :  
  
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0  

我的具体代码如下:

如下,我有一个类和一个测试类。

package com.jiaotong114;

public class Calculator {
    public int divide(int a, int b) throws Exception {
        if(0 == b) {
            throw new Exception("除数不能为0");
        }
        return a / b;
    }
}
package java.com.jiaotong114;

import org.junit.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.jiaotong114.Calculator;

public class CalculatorTest3 {

    private Calculator calculator = null;
    @Before
    public void setUp() throws Exception
    {
        System.out.println("set up");
        // 生成成员变量的实例
        calculator = new Calculator();
        System.out.println(calculator);
    }
    @After
    public void tearDown() throws Exception
    {
        System.out.println("tear down");
    }

    @Test
    public void TestDivide()
    {
        int result = 0;
        try
        {
            result = calculator.divide(12, 3);
        }
        catch (Exception e)
        {
            e.printStackTrace();

            // 如果抛出异常,证明测试失败,没有通过,没通过的测试计数在Failures中
            Assert.fail();
            // 如果不加这一行,如果程序进入到catch,无法判断其失败
        }
        // 判断方法的返回结果
        Assert.assertEquals(4, result);// 第一个参数是期望值,第二个参数是要验证的值

    }
    @Test
    public void TestDivideByZero()
    {
        Throwable tx = null;
        Assert.fail("没有抛出异常,测试失败");
        int result = 0;
        try
        {
            result = calculator.divide(12, 0);

            Assert.fail("没有抛出异常,测试失败");// 如果执行到这行代码,则证明没有抛出异常,说明我们的验证失败
        }
        catch (Exception e)
        {
            e.printStackTrace();
            tx = e;

        }

        Assert.assertEquals(Exception.class, tx.getClass());// 抛出的异常类型是否和期望一致
        Assert.assertEquals("除数不能为0", tx.getMessage());// 抛出的异常信息是否和期望一致

        //如果上面两个都通过,则测试通过

    }

}

我的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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jiaotong114</groupId>
  <artifactId>jiaotong</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>jiaotong Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.3.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>jiaotong</finalName>
  </build>
</project>

运行mvn test时控制台输出:

"C:\Program Files\Java\jdk1.8.0_65\bin\java" -Dmaven.home=C:\zhangfeng\program\apache-maven-3.2.2 -Dclassworlds.conf=C:\zhangfeng\program\apache-maven-3.2.2\bin\m2.conf -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\zhangfeng\program\apache-maven-3.2.2\boot\plexus-classworlds-2.5.1.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=2016.2.4 test
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building jiaotong Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jiaotong ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ jiaotong ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jiaotong ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\zhangfeng\program\ideaProjects\jiaotong114\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ jiaotong ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\zhangfeng\program\ideaProjects\jiaotong114\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jiaotong ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.697 s
[INFO] Finished at: 2016-10-19T13:21:57+08:00
[INFO] Final Memory: 13M/155M
[INFO] ------------------------------------------------------------------------

项目结构
图片描述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

肤浅与狂妄 2022-09-11 04:39:50
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <configuration>
                <includes>
                    <include>XXXTests.java</include>
                </includes>
            </configuration>
        </plugin>

是不是需要加上这个。

等往事风中吹 2022-09-11 04:39:50

mvn test 使用的是surefire插件
它按照指定格式的类名来查找匹配的测试类
默认包含的测试类:
*/Test.java
*/Test.java
*/TestCase.java
默认排除的测试类:
*/AbstractTest.java
*/AbstractTestCase.java

以为你会在 2022-09-11 04:39:50

对的,我是这么解决的

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文