zoukankan      html  css  js  c++  java
  • junit+maven单元测试

    一、概念

    junit是一个专门测试的框架

    集合maven进行单元测试,可批量测试类中的大量方法是否符合预期


    二、作用:
    单元测试:测试的内容是类中的方法,每一个方法都是独立测试的。方法是测试的基本单位。


    三、使用方法
    1、pom内加入依赖

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

    2、在maven中src/test/java目录下的,创建测试程序。
    推荐的创建类和方法的提示:
    1、测试类的名称:Test+待测试类名
    2、测试方法的名称:Test+方法名称
    例如:你要测试HelloMaven
    创建测试类TestHelloMaven
    @Test
    public void testaDD(){
    测试HelloMaven的add方法是否正确
    }
    其中testAdd叫做测试方法,定义规则:
    1、方法必须是public的
    2、方法必须没有返回值
    3、方法名称自定义,推荐是Test+被测方法
    4、方法上面加上注解@Test

     

    四、举例,Hello项目

    1、新建java源程序,存放在Hellosrcmainjavacom estbk目录下,取名HelloMaven.java

    package com.testbk;
    import org.junit.Assert;
    import org.junit.Test;
    
    public class TestHelloMaven{
        @Test
        public void testAdd(){
            System.out.println("maven junit testAdd()===")
            HelloMaven hello = new HelloMaven();
            int res = hello.add(10,20);
            //验证10+20是不是30,juit提供的方法,对比结果的
            //assertEquals(期望值,实际值)
            Assert.assertEquals(30,res)
        }
    }

    2、新建maven测试类型,存放在Hellosrcmainjavacom estbk目录下,取名TestHelloMaven.java

    package com.testbk;
    import org.junit.Assert;
    import org.junit.Test;
    
    public class TestHelloMaven{
        @Test
        public void testAdd(){
            System.out.println("maven junit testAdd()===")
            HelloMaven hello = new HelloMaven();
            int res = hello.add(10,20);
            //验证10+20是不是30,juit提供的方法,对比结果的
            //assertEquals(期望值,实际值)
            Assert.assertEquals(30,res)
        }
        @Test
        public void testAdd2(){
            System.out.println("#####maven junit testAdd()2###");
            HelloMaven hello = new HelloMaven();
            int res = hello.add(10,20);
            //验证10+20是不是30,juit提供的方法,对比结果的
            //assertEquals(期望值,实际值)
            Assert.assertEquals(50,res);
        }
    }

    3、执行mvn clean:清理target目录

    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------< com.testbk:testjava >-------------------------
    [INFO] Building maven 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ testjava ---
    [INFO] Deleting D:javaProjectsHello	arget
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  0.196 s
    [INFO] Finished at: 2021-04-25T22:50:10+08:00
    [INFO] ------------------------------------------------------------------------

    4、执行mvn compile:编译main/java目录下的java为class文件,同时把class拷贝到target/classes目录下面

    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------< com.testbk:testjava >-------------------------
    [INFO] Building maven 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testjava ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testjava ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to D:javaProjectsHello	argetclasses
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  0.724 s
    [INFO] Finished at: 2021-04-25T22:54:23+08:00
    [INFO] ------------------------------------------------------------------------

    5、执行mvn test-compile:编译test/java目录下的java为class文件,同时class拷贝到target/test-classes目录下面

    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------< com.testbk:testjava >-------------------------
    [INFO] Building maven 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testjava ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testjava ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testjava ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testjava ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to D:javaProjectsHello	arget	est-classes
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  0.764 s
    [INFO] Finished at: 2021-04-25T22:55:43+08:00
    [INFO] ------------------------------------------------------------------------

    6、执行mvn test:查看测试结果,通过1,失败1,并在指定目录生成测试报告

    Results :
    
    Failed tests:   testAdd2(com.testbk.TestHelloMaven): expected:<50> but was:<30>
    
    Tests run: 2, Failures: 1, Errors: 0, Skipped: 0
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  1.050 s
    [INFO] Finished at: 2021-04-25T22:57:16+08:00
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project testjava: There are test failures.
    [ERROR]
    [ERROR] Please refer to D:javaProjectsHello	argetsurefire-reports for the individual test results.
    [ERROR] -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

    7、修改测试代码,并再次执行mvn test

    package com.testbk;
    import org.junit.Assert;
    import org.junit.Test;
    
    public class TestHelloMaven{
        @Test
        public void testAdd(){
            System.out.println("=====maven junit testAdd()===");
            HelloMaven hello = new HelloMaven();
            int res = hello.add(10,20);
            //验证10+20是不是30,juit提供的方法,对比结果的
            //assertEquals(期望值,实际值)
            Assert.assertEquals(30,res);
        }
        @Test
        public void testAdd2(){
            System.out.println("#####maven junit testAdd()2###");
            HelloMaven hello = new HelloMaven();
            int res = hello.add(30,20);
            //验证10+20是不是30,juit提供的方法,对比结果的
            //assertEquals(期望值,实际值)
            Assert.assertEquals(50,res);
        }
    }

    查看运行结果,测试通过

     T E S T S
    -------------------------------------------------------
    Running com.testbk.TestHelloMaven
    =====maven junit testAdd()===
    #####maven junit testAdd()2###
    Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec
    
    Results :
    
    Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  1.282 s
    [INFO] Finished at: 2021-04-25T22:58:41+08:00
    [INFO] ------------------------------------------------------------------------
  • 相关阅读:
    关于MIME类型问题,浏览器请求到的资源是乱码
    关于vue切换用户,路由表不更新问题
    Flutter环境安装,ios真机调试
    vue使用trackingjs
    js的中文英文排序
    google无法播放mp4 chrome无法播放h264
    写在Blog前
    NOI2010Day1
    菜鸡KLC的中考游记
    ABC161 题解
  • 原文地址:https://www.cnblogs.com/mrwhite2020/p/14695991.html
Copyright © 2011-2022 走看看