zoukankan      html  css  js  c++  java
  • 走进JavaWeb技术世界11:单元测试框架Junit

     

    JUnit你不知道的那些事儿

    转自 老刘 码农翻身 2016-02-24

    话说有一次Eric Gamma 坐飞机的时候偶遇Kent Beck(对,就是极限编程和TDD的发起人) ,  两位大牛见面寒暄过以后就觉得很无聊了。

    旅途漫漫,干点啥好呢。

    Kent Beck当时力推测试驱动开发,  但是没有一个工具或者框架能让大家轻松愉快的写测试,并且自动的运行测试。

    两人勾兑了一下:不如自己挽起袖子写一个, 于是两人就在飞机上结对编程 ,  等到飞机的时候,一个划时代的单元测试工具就新鲜出炉了,这就是JUnit:

    JUnit当然是用Java写的, 其他语言一看, 这东西好, 咱也搞一套, 于是就有了一大批工具,统称xUnit

    NUnit (针对.Net 平台) ,  CUnit (针对C语言) , CppUnit(针对C++语言), PyUnit (针对Python), PHPUnit, OCUnit, DUnit, JSUnit ......

    超级大牛出马,亲自敲出来的代码自然非同凡响, 它的设计简直就是使用设计模式的典范:

    更牛的是, 他们是以一种叫做“模式生成架构”的方法来创建JUnit的, 换句话说,就是从零应用模式, 然后一个接一个, 直到你获取最终的系统架构:

    我第一次看到这种方式, 真是惊为天人,  我知道代码要向模式进行重构, 还从来没听过由模式来构建系统!

    但一想到Eric Gamma的背景, 就没有什么可惊讶的了, 因为Eric Gamma 实际上是划时代的书籍《设计模式:可复用面向对象软件基础》四位合著者(称为GoF,Gang of Four)之一

    这本书的经典地位就不用说了, 像JUnit繁衍出来的xUnit一样, 这本书也有很多的“繁衍品”, 例如

    《Head First Degisn Pattern》 , 《设计模式解析》,《大话设计模式》。。。。

    JUnit超级流行,几乎是事实上的Java 单元测试和TDD的工具标准, 有人选择了GitHub上最热的三门语言Java,Ruby和Javascript , 对每个语言分析了1000个项目,找出了最常用的组件,可以看到JUnit 以30.7%并列第一

    所以JUnit已经变成了程序员必备的技能, 不会JUnit就太Out了。

     

    Junit入门

    1、什么是Junit4

    JUnit4是一个易学易用的Java单元测试框架,一般我们在写完一段代码或一个方的时候,都要测试一下这段代码和这个方法的逻辑是不是正确,输入一定的数据,返回的数据是不是我们想要的结果,即我们在写单个业务代码针对结果进行测试。这时Junit就派上了大用场了。

    2、为何使用Junit4

    也许有的初学者会说,项目完成之后测试不行吗?如果你要这么想的话,那就了,因为随着你代码的增加,你牵扯到的模块和项目中的逻辑就会越来越多,这样测试起来就会非常的麻烦,而且容易出错。Junit看起来是增加了代码量,可是它可以大大的减少后期的测试时间,提升代码的质量,让代码更易于维护。

    3、Junit4的快速入门

    下载并导入Junitjar包:

    首先得需要去网上下载一个Junit4的一个jar包,保存到自己本地电脑里面打开myEclipse新建一个Java项目,通过右击项目-->Build Path --->Configure Build Path -->Add External JARs--->找到自己刚保存的jar路径,点击OK就可以啦

    创建测试目录:

    接下来就要为我们的测试建立特殊的路径,这个特殊特殊在哪里呢,因为我们的测试代码要单独保存不能与源代码进行混淆,到项目结束后可以直接把它删除,而且不会对项目造成影响。怎么创建呢:右击项目名称--->new---->suorce folder--->输入你想要的测试文件名点击OK就可以啦。

    接下来我们得在项目中的src目录下创建一个包,注意这儿的包名和test里面的包名要保持一致,在我们src的包下写我们项目的逻辑方法,在test的报下写我们的测试方法,结构如图所示,

    image.png

    下面我们来写一段简单的逻辑代码进行测试演练

    
    package com.junit;
    
    public class method_junti {
        public int add(int a, int b) {
            return a + b;
        }
    
        public int subtract(int a, int b) {
            return a - b;
        }
    
        public int multiply(int a, int b) {
            return a * b;
        }
    
        public int division(int a, int b) {
            return a / b;
        }
    }
    
    

    方法很简单,就是一般的加减乘除,下面我们就可以进行测试了,怎么测试呢: 在我们的测试目录下新建测试类junit_test,然后定义测试方法。代码如下:

    
    package com.junit;
    import static org.junit.Assert.*;
    import org.junit.Test;
    public class junit_test {
    
        
        //测试方法必须有@test;
        //该测试方法必须由public void修饰,没有返回值;
        //该方法不带任何参数;
        //新建一个源代码测试文件单独存放测试代码;
        //测试类的包和被测试类的包保持一致;
        //测试方法间互相独立没有任何依赖;
        @Test
        public void testAdd(){
            
            assertEquals(4, new method_junti().add(3, 0));
        }
        
        @Test
        public void testSubtract(){
            assertEquals(3, new method_junti().subtract(6, 3));
        }
        
        @Test
        public void testMultiply(){
            assertEquals(6, new method_junti().multiply(6, 1));
        }
        
        @Test
        public void testDivision(){
            assertEquals(6, new method_junti().division(6, 1));
        }
    }
    

    下面来讲解一下assertEquals()这个函数,它的第一个参数是你预期的一个结果,第二个参数使我们想测试的函数,这个函数我们要通过先new出函数所在的类,然后通过类来调用方法,方法里面的参数就是你在写该方法是传进来的参数。在这里最好在你需要测试的方法的前面加上test,这样的话就会比较规范一些

    写完之后你可以点击测试类,然后在点击run as,再点击Junit test,你就能在弹出的窗口中看到你的测试结果,它会提示你失败的个数和错误的个数。如果你只想测试一个方法怎么办呢,在你创建的测试类的下面还有目录,列表里面的会列出你所有的测试方法,你就可以右击你想测试的方法,然后run as ---> junit test,测试成功后就会看到一个绿色的条,结果如图:

    image.png

    在这里如果我们每一个方法都要自己手动的敲出它的测试方法,在这里我们只是简单的测试了几个方法,在项目中如果我们有很多的方法需要测试,一个一个的敲的话会有些浪费时间了,这里给大家介绍一个快速生成测试方法的一个方法:
    点击src下的method_junit.java--->右击new--->看看后面的提示框中有么有Junit test case,如果没有的话点击other,在提示框中输入Junit 就会出现--->在弹出的对话框中找到Source folder,点击后面的Browse将其改为含有test的那个目录,这里有些可能会提示名字重复,把下面的 Name改改就行--->点击next--->你会看到method_junit里面的所有法,这时候你就可以选中它们,成成测试方法如下:

    
    package com.junit;
    import static org.junit.Assert.*;
    import org.junit.Test;
    public class method_juntiTest2 {
    
        @Test
        public void testAdd() {
            fail("Not yet implemented");
        }
    
        @Test
        public void testSubtract() {
            fail("Not yet implemented");
        }
    
        @Test
        public void testMultiply() {
            fail("Not yet implemented");
        }
    
        @Test
        public void testDivision() {
            fail("Not yet implemented");
        }
    }
    

    再把里面fail后面的语句删了就可以写你想测试的东西呢!

    4、junit测试失败的两种情况:

    在前面的情况中我们都是测试的是成功的例子,但是Junit的作用只是测试的方法里的返回数据是不是正确的,但是在数据返回正确的情况下我们未必是正确的,就比如如果你要求的是长方形的面积,但是你用的是周长公式,当你在测试的时候他也会给你测试成功,得到预期的结果,也就是说我们的测试用例对于逻辑错误是无能为力的

    测试失败的情况一
    当我们预期值和程序执行的结果不一样的时候就会测试失败:
    比如我们上面的测试加法函数的方法:

    
    @Test
    public void testAdd(){
          assertEquals(3, new method_junti().add(3, 0));
    }
    

    如果把预期结果3,改为4,就会测试失败,failure的后面就会出现一个1,提示一个测试失败,运行时就能看到,这个应该不难理解。如果你仔细观察的话,下面还会有相关的提示,如图所示:

    image.png

    测试失败情况二:
    下面我们在来测试除法:

    
    @Test
    public void testDivision(){
        assertEquals(6, new method_junti().division(6, 1));
    }
    

    如果在这里把除数改为0,会出现什么情况呢:后面的提示框中的error数就会变1;提示有一个错误。于是我们得出下面的两种情况:

    1、failure一般由单元测试使用的断言方法判断失败所引起的,这表示测试点发现了问题,也就是说程序输出的结果和预期的不一样
    2、error是由代码异常引起的,他可能是由于测试代码本身的错误,也可能是被测试代码中隐藏的一个bug

    5、Junit的运行流程:

    首先我们先在test包下的com.junit新建一个junit case,和并且直接命名junit_case。在创建的时候把setUpBeforeClass(),tearDownAfterClass(),setUp() ,tearDown() 选上就会得到下面的代码:

    
    package com.junit;
    
    import org.junit.After;
    import org.junit.AfterClass;
    import static org.junit.Assert.*;
    
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    
    public class Junit_case {
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
        }
    
        @AfterClass
        public static void tearDownAfterClass() throws Exception {
        }
    
        @Before
        public void setUp() throws Exception {
        }
    
        @After
        public void tearDown() throws Exception {
        }
    
        @Test
        public void test() {
            fail("Not yet implemented");
        }
    }
    

    下面我们在每个方法中输入一句简单的输出语句,看看他们的运行状态,如下:

    
    package com.junit;
    
    import static org.junit.Assert.*;
    
    import org.junit.After;
    import org.junit.AfterClass;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    public class Junit_test1 {
    
        
        /* 1、BeforeClass修饰的方法会在所有方法被调用前执行
         * 而且该方法是静态的,所以当测试类被加载后接着就执行它
         * 在内存中它只会存在一份,适合加载配置文件
         * 2、AfterClass修饰的方法用来对资源的清理,如关闭数据库的连接
         * befoer和after修饰的方法在每个test修饰的方法执行前会被各执行一次,假如有两个
         * test文件,before和after会被各执行两次;
         * */
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
            System.out.println("this is beforeclass");
        }
    
        @AfterClass
        public static void tearDownAfterClass() throws Exception {
            System.out.println("this is afterclass");
        }
    
        @Before
        public void setUp() throws Exception {
            System.out.println("this is before");
        }
    
        @After
        public void tearDown() throws Exception {
            System.out.println("this is after");
        }
    
        @Test
        public void test1() {
            System.out.println("this is test1");
        }
    
    }
    
    

    如果运行上面的代码,就会得到下面的结果

    6、junit的常用注解:

    上面我已经讲解了@test,@BeforeClass,@AfterClass,@Before,@After这些注解的详解可见这位仁兄的博客,下面提供相关地址:http://blog.csdn.net/zixiao217/article/details/52951679
    对于@test,他除了将一个普通的方法修饰为测试方法外,还可以处理异常,设置超时。下面来对test的异常处理做讲解test有两个参数:expected和timeout,即异常处理和设置超时如果对我们上面的除数为0的那个方法进行异常处理,那么我们就可以看到代码能够正常,测试通过,代码如下:

    
    @Test(expected=ArithmeticException.class)
    public void testDivision(){
         assertEquals(6, new method_junti().division(6, 0));
    }
    

    在测试一些对性能有要求的方法中设置超时是很有必要的,它可以检测你的代码能否在这个
    时间段内运行出结果,设置方法如下:

    
    @Test(timeout=2000)//单位是毫秒
     public void testWhile(){
        while(true){
            System.out.println("run forever");
        }
    }
    

    @Ignore:在test方法上加上该修饰,测试的时候就会不执行该测试方法;
    @RunWith可以更改测试运行器;我们除了使用junit提供的测试运行器之外,还可以自定义
    我们的运行器,只要继承org.junit.runner.Runner
    代码如下:

    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Suite;
    import org.junit.runners.Suite.SuiteClasses;
    
    @RunWith(Suite.class)
    @Suite.SuiteClasses({TaskTest1.class,TaskTest2.class,TaskTest3.class})
    public class SuitTest {
        
        public void test(){
            /*
             * 由于在开发的项目中,测试的类很多,一个一个运行很浪费时间,于是可以写一个测试
             * 套件把所有需要测试的类组合在一起测试运行
             * 1、写一个测试入口,这个类不含其它的方法;
             * 2、更改测试运行器@RunWith(Suite.class)
             * 3、将要测试的类作为数组放在@Suite.SuiteClasses({})中;
             */
            
        }
    }
    

    7、junit的参数设置

    在上面的测试中,我们对一个方法都是只测试了一组数据,可是在真正的项目中,一组数据往往是不够的,我们需要很多组数据,如果每一组数组写一个测试方法的话那可把我们的工作人员累死了!这时我们可以使用参数设置来解决这个问题。
    代码如下:

    
    package com.junit;
    import static org.junit.Assert.*;
    
    import java.util.Arrays;
    import java.util.Collection;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    
    @RunWith(Parameterized.class)
    public class ParameterTest {
        //声明变量存放预期值和测试数据;
        int expected=0;
        int input1=0;
        int input2=0;
        
        @Parameters
        public static Collection<Object[]> test(){
        
            return Arrays.asList(new Object[][]{
                    {3,1,2},
                    {4,2,2}
                });     
        }
    
        public ParameterTest(int expected,int input1,int input2){
            this.expected=expected;
            this.input1=input1;
            this.input2=input2;
        }
        @Test
        public void testAdd(){
            assertEquals(expected, new method_junti().add(input1, input2));
        }   
    }
    

    我们需要测试多组数据,那么我们就需要用到数组来存放多组数据,这里用Arrays.asList来接收。

    聊聊单元测试在SpringMVC中的运用

    转自 杜琪 

    遇到问题多思考、多查阅、多验证,方能有所得,再勤快点乐于分享,才能写出好文章。

    一、单元测试

    1. 定义与特点

    单元测试(unit testing):是指对软件中的最小可测试单元进行检查和验证。

    这个定义有点抽象,这里举几个单元测试的特性,大家感受一下:一般是一个函数配几个单元测试、单元测试不应该依赖外部系统、单元测试运行速度很快、单元测试不应该造成测试环境的脏数据、单元测试可以重复运行。

    2. 优点

    单元测试使得我们可以放心修改、重构业务代码,而不用担心修改某处代码后带来的副作用。

    单元测试可以帮助我们反思模块划分的合理性,如果一个单元测试写得逻辑非常复杂、或者说一个函数复杂到无法写单测,那就说明模块的抽象有问题。

    单元测试使得系统具备更好的可维护性、具备更好的可读性;对于团队的新人来说,阅读系统代码可以从单元测试入手,一点点开始后熟悉系统的逻辑。

    3. 本文要解决的痛点

    1. 单测何时写?
      如果你的团队在坚持TDD的风格,那就是在编码之前写;如果没有,也不建议在全部业务代码编写完成之后再开始补单元测试。单元测试比较(最)合适的时机是:一块业务逻辑写完后,跟着写几个单元测试验证下。
    2. 单测怎么写?
      分层单测:数据库操作层、中间件依赖层、业务逻辑层,各自的单元测试各自写,互相不要有依赖。
    3. 单测运行太慢?
    • dao层测试,使用H2进行测试,做独立的BaseH2Test、独立的test-h2-applicationContext.xml,只对dao的测试
    • service层测试,依赖mockito框架,使用@RunWith(MockitoJUnitRunner.class)注解,就无需加载其他spring bean,具体用法
    • 对于依赖外部的中间件(例如redis、diamond、mq),在处理单测的时候要注意分开加载和测试,尤其是与dao的测试分开

    二、Spring项目中的单元测试实践

    我们基于unit-test-demo这个项目进行单元测试的实践。

    1. dao层单元测试

    最开始写单测的时候,要连着DEV的数据库,这时候会有两个烦恼:网络有问题的时候单测运行不通过、数据库里造成脏数据的时候会导致应用程序异常。这里我们选择H2进行DAO层的单元测试。有如下几个步骤:

    • 在resources下新建目录h2,存放schema.sql和data-prepare-user.sql文件,前者用于保存建表语句,后者用于准备初始数据
    • test-data-source.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:jdbc="http://www.springframework.org/schema/jdbc"   xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/jdbc
           http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
    
    
        <!-- 初始化数据表结构 -->
        <jdbc:initialize-database data-source="dataSource">
            <jdbc:script location="classpath:h2/schema.sql" encoding="UTF-8"/>
            <jdbc:script location="classpath:h2/data-prepare-*.sql" encoding="UTF-8"/>
        </jdbc:initialize-database>
    
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
              init-method="init" destroy-method="close">
            <property name="url" value="${user.jdbc.url}"/>
            <property name="username" value="${user.jdbc.username}"/>
            <property name="password" value="${user.jdbc.password}"/>
            <!-- 连接池初始连接数 -->
            <property name="initialSize" value="3"/>
            <!-- 允许的最大同时使用中(在被业务线程持有,还没有归还给druid) 的连接数 -->
            <property name="maxActive" value="30"/>
            <!-- 允许的最小空闲连接数,空闲连接超时踢除过程会最少保留的连接数 -->
            <property name="minIdle" value="3"/>
            <!-- 从连接池获取连接的最大等待时间 5 秒-->
            <property name="maxWait" value="5000"/>
    
            <!-- 强行关闭从连接池获取而长时间未归还给druid的连接(认为异常连接)-->
            <property name="removeAbandoned" value="true"/>
            <!-- 异常连接判断条件,超过180 秒 则认为是异常的,需要强行关闭 -->
            <property name="removeAbandonedTimeout" value="180"/>
    
            <!-- 从连接池获取到连接后,如果超过被空闲剔除周期,是否做一次连接有效性检查 -->
            <property name="testWhileIdle" value="true"/>
            <!-- 从连接池获取连接后,是否马上执行一次检查 -->
            <property name="testOnBorrow" value="false"/>
            <!-- 归还连接到连接池时是否马上做一次检查 -->
            <property name="testOnReturn" value="false"/>
            <!-- 连接有效性检查的SQL -->
            <property name="validationQuery" value="SELECT 1"/>
            <!-- 连接有效性检查的超时时间 1 秒 -->
            <property name="validationQueryTimeout" value="1"/>
    
            <!-- 周期性剔除长时间呆在池子里未被使用的空闲连接, 10秒一次-->
            <property name="timeBetweenEvictionRunsMillis" value="10000"/>
            <!-- 空闲多久可以认为是空闲太长而需要剔除 30 秒-->
            <property name="minEvictableIdleTimeMillis" value="30000"/>
    
            <!-- 是否缓存prepareStatement,也就是PSCache,MySQL建议关闭 -->
            <property name="poolPreparedStatements" value="false"/>
            <property name="maxOpenPreparedStatements" value="-1"/>
    
            <!-- 是否设置自动提交,相当于每个语句一个事务 -->
            <property name="defaultAutoCommit" value="true"/>
            <!-- 记录被判定为异常的连接 -->
            <property name="logAbandoned" value="true"/>
            <!-- 网络读取超时,网络连接超时 -->
            <property name="connectionProperties" value="connectTimeout=1000;socketTimeout=3000"/>
        </bean>
    
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>
            <property name="typeAliasesPackage" value="org.learnjava.dq.core.dal.bean"/>
        </bean>
    
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="org.learnjava.dq.core.dal.dao"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        </bean>
    </beans>
    
    • test-h2-applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 激活自动代理功能 -->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
        <!-- spring容器启动时,静态配置替换 -->
        <context:property-placeholder location="classpath*:*.properties" ignore-unresolvable="true"/>
    
        <context:component-scan base-package="org.learnjava.dq.core.dal.dao"/>
    
        <import resource="test-data-sources.xml"/>
    </beans>
    
    • UserInfoDAOTest
      这个文件是DAO层单元测试的主要内容,我只写了一个,读者朋友可以下载代码自己练习,把剩余的几个写了。

    PS:这里我们只有一个DAO,所以spring容器加载就放在这个文件里了,如果DAO多的话,建议抽出一个BaseH2Test文件,这样所有的DAO单元测试只需要加载一次spring容器。

    
    package org.learnjava.dq.core.dal.dao;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.learnjava.dq.core.dal.bean.UserInfoBean;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import java.util.Date;
    import javax.annotation.Resource;
    import static org.junit.Assert.*;
    
    /**
     * 作用:
     * User: duqi
     * Date: 2017/6/24
     * Time: 09:33
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:test-h2-applicationContext.xml")
    public class UserInfoDAOTest {
    
        @Resource
        private UserInfoDAO userInfoDAO;
    
        @Test
        public void saveUserInfoBean() throws Exception {
            UserInfoBean userInfoBean = new UserInfoBean();
            userInfoBean.setUserId(1003L);
            userInfoBean.setNickname("wangwu");
            userInfoBean.setMobile("18890987675");
            userInfoBean.setSex(1);
            userInfoBean.setUpdateTime(new Date());
            userInfoBean.setCreateTime(new Date());
    
            int rows = userInfoDAO.saveUserInfoBean(userInfoBean);
    
            assertEquals(1, rows);
        }
    
        @Test
        public void updateUserInfoBean() throws Exception {
        }
    
        @Test
        public void getUserInfoBeanByUserId() throws Exception {
        }
    
        @Test
        public void getUserInfoBeanByMobile() throws Exception {
        }
    
        @Test
        public void listUserInfoBeanByUserIds() throws Exception {
        }
    
        @Test
        public void removeUserInfoBeanByUserId() throws Exception {
        }
    
    }
    

    2. service层单元测试

    • Mockito
      Mocktio是一个非常易用的mock框架。开发者可以依靠Mockito提供的简洁的API写出漂亮的单元测试。

    Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with a clean & simple API. Mockito doesn’t give you hangover because the tests are very readable and they produce clean verification errors.

    • UserInfoManagerImplTest
      单元测试,不应该依赖于DAO层的执行逻辑是否正确【否则就是集成测试】,需要假设DAO的行为是什么样子,然后再看本层的逻辑是否正确。
      这里使用@RunWith(MockitoJUnitRunner.class)修饰当前的单元测试类,如果有多个单元测试类的话,可以考虑抽出一个基础的BaseBizTest类。
    
    package org.learnjava.dq.biz.manager.impl;
    
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.learnjava.dq.biz.domain.UserInfo;
    import org.learnjava.dq.biz.manager.UserInfoManager;
    import org.learnjava.dq.core.dal.bean.UserInfoBean;
    import org.learnjava.dq.core.dal.dao.UserInfoDAO;
    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import org.mockito.MockitoAnnotations;
    import org.mockito.runners.MockitoJUnitRunner;
    
    import static org.junit.Assert.*;
    
    import static org.mockito.Mockito.*;
    
    /**
     * 作用:
     * User: duqi
     * Date: 2017/6/24
     * Time: 09:55
     */
    @RunWith(MockitoJUnitRunner.class)
    public class UserInfoManagerImplTest {
    
        @Mock //用于定义被Mock的组件
        private UserInfoDAO userInfoDAO;
    
        @InjectMocks //用于定义待测试的组件
        private UserInfoManager userInfoManager = new UserInfoManagerImpl();
    
        private UserInfo userInfoToSave;
    
        @Before
        public void setUp() throws Exception {
            //用于初始化@Mock注解修饰的组件
            MockitoAnnotations.initMocks(this);
    
            userInfoToSave = new UserInfo();
            userInfoToSave.setMobile("18978760099");
            userInfoToSave.setUserId(7777L);
            userInfoToSave.setSex(1);
        }
    
        @Test
        public void saveUserInfo_case1() throws Exception {
            //step1 准备数据和动作
            doReturn(1).when(userInfoDAO).saveUserInfoBean(any(UserInfoBean.class));
    
            //step2 运行待测试模块
            Boolean res = userInfoManager.saveUserInfo(userInfoToSave);
    
            //step3 验证测试结果
            assertTrue(res);
        }
    
        @Test
        public void saveUserInfo_case2() throws Exception {
            //step1 准备数据和动作
            doReturn(0).when(userInfoDAO).saveUserInfoBean(any(UserInfoBean.class));
    
            //step2 运行待测试模块
            Boolean res = userInfoManager.saveUserInfo(userInfoToSave);
    
            //step3 验证测试结果
            assertFalse(res);
        }
    
        @Test
        public void updateUserInfo() throws Exception {
        }
    
        @Test
        public void getUserInfoByUserId() throws Exception {
        }
    
        @Test
        public void getUserInfoByMobile() throws Exception {
        }
    
        @Test
        public void listUserInfoByUserIds() throws Exception {
        }
    
        @Test
        public void removeUserInfoByUserId() throws Exception {
        }
    
    }
    
    • Mockito要点
      • MockitoJUnitRunner:用于提供单元测试运行的容器环境
      • Mock:用于模拟待测试模块中依赖的外部组件
      • InjectMock:用于标识待测试组件
      • org.mockito.Mockito.*:这个类里的方法可以用于指定Mock组件的预期行为,包括异常处理。

    三、总结

    1. 单元测试的三个步骤
    • 准备数据、行为
    • 测试目标模块
    • 验证测试结果
    1. 除了本文中提到的Junit、Mockito、H2,还有很多其他的单元测试框架,例如TestNGspock等。
    2. 在Java Web项目中,controller层一般不写业务逻辑,也就没有必要写单元测试,但是如果要写,也有办法,可以参考我之前的文章:在Spring Boot项目中使用Spock框架
    3. 单元测试代码也是线上代码,要和业务代码一样认真对待,也需要注意代码和测试数据的复用。
    一位阿里 Java 工程师的技术小站。作者黄小斜,专注 Java 相关技术:SSM、SpringBoot、MySQL、分布式、中间件、集群、Linux、网络、多线程,偶尔讲点Docker、ELK,同时也分享技术干货和学习经验,致力于Java全栈开发!(关注公众号后回复”Java“即可领取 Java基础、进阶、项目和架构师等免费学习资料,更有数据库、分布式、微服务等热门技术学习视频,内容丰富,兼顾原理和实践,另外也将赠送作者原创的Java学习指南、Java程序员面试指南等干货资源)

    微信公众号【Java技术江湖】一位阿里 Java 工程师的技术小站。(关注公众号后回复”Java“即可领取 Java基础、进阶、项目和架构师等免费学习资料,更有数据库、分布式、微服务等热门技术学习视频,内容丰富,兼顾原理和实践,另外也将赠送作者原创的Java学习指南、Java程序员面试指南等干货资源)

  • 相关阅读:
    Django
    索引
    idea启动tomcat后控制台日志显示中文乱码问题
    Elasticsearch
    Hive 开窗函数
    hadoop-3.1.1集群搭建
    hadoop-2.6.5集群搭建
    spark集群搭建(spark版本2.3.2)
    Zookeeper
    spark-submit(spark版本2.3.2)
  • 原文地址:https://www.cnblogs.com/xll1025/p/11366429.html
Copyright © 2011-2022 走看看