zoukankan      html  css  js  c++  java
  • PowerMock学习(七)之Mock Constructor的使用

    前言

    我们在编码的时候,总习惯在构造器中传参数,那么在powermock中是怎么模拟带参数构造的呢,这并不难。

    模拟场景

    我们先模拟这样一个场景,通过dao中的传入一个是布尔类型(是否加载)和一个枚举类(使用哪种数据库),构造Dao这个类,在编写一个插入学生方法

    dao部分的代码

    具体示例代码如下:

    package com.rongrong.powermock.mockconstructor;
    
    /**
     * @author rongrong
     * @version 1.0
     * @date 2019/11/28 23:12
     */
    public class StudentConstructorDao {
        public enum DataBaseType{
            MYSQL,
            ORACLE,
        }
    
        /**
         *
         * @param isLoad 数据库是否加载即链接
         * @param dataBaseType 数据库类型
         */
        public StudentConstructorDao(Boolean isLoad,DataBaseType dataBaseType) {
            throw new UnsupportedOperationException();
        }
    
        public void insertStudent(StudentConstructor studentConstructor){
            throw new UnsupportedOperationException();
        }
    }

    service部分代码

    用来调用dao中的函数,具体代码如下:

    package com.rongrong.powermock.mockconstructor;
    
    /**
     * @author rongrong
     * @version 1.0
     * @date 2019/11/28 23:18
     */
    public class StudentConstructorService {
    
        public void createStudnet(StudentConstructor studentConstructor){
            StudentConstructorDao studentConstructorDao = new StudentConstructorDao(false, StudentConstructorDao.DataBaseType.MYSQL);
            studentConstructorDao.insertStudent(studentConstructor);
        }
    }

    编写测试用例

    使用powermock去模拟构造函数,并测试这个service,具体代码如下:

    package com.rongrong.powermock.mockconstructor;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mockito;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    
    import static com.rongrong.powermock.mockconstructor.StudentConstructorDao.DataBaseType.MYSQL;
    
    /**
     * @author rongrong
     * @version 1.0
     * @date 2019/11/28 23:54
     */
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(StudentConstructorService.class)
    public class TestStudentConstructorService {
    
        @Test
        public void testStudentConstructorService(){
            StudentConstructorDao studentConstructorDao = PowerMockito.mock(StudentConstructorDao.class);
            try {
                //此处需要注释下,需要构造一个带参数的Dao对象,即便是假的也要带参数,该类初始化的时候就带参数
                PowerMockito.whenNew(StudentConstructorDao.class).withArguments(false,MYSQL).thenReturn(studentConstructorDao);
                StudentConstructor studentConstructor = new StudentConstructor();
                StudentConstructorService studentConstructorService = new StudentConstructorService();
                studentConstructorService.createStudnet(studentConstructor);
                Mockito.verify(studentConstructorDao).insertStudent(studentConstructor);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    总结

    • 首先mock了一个StudentConstructorDao对象实例
    • 使用whenNew语法,传入必须入参,注意这里的参数必须和Service中的参数一致,否则会在Service中还会继续创建一个新的StudentConstructorDao实例
    • 接着验证方法的调用

    注意:当不能确定构造器中参数时,使用withAnyArguments()即可,关于Student类代码,参照之前文章Student类,此处略

  • 相关阅读:
    Enterprise Library3.1 使用数据访问模块时,调用Microsoft.Practices.EnterpriseLibrary.Data报出源文件与当前应用程序不一致和创建dataconfiguration的配置节处理程序出错
    net精华:C#中对注册表的操作
    [翻译]使用Enterprise Library 3.0的日志程序块
    分布式应用程序概述
    调整Oracle数据库print_bill表字段BillMKID的顺序,并判断表print_bill是否存在及字段billMKID是否存在
    Win32下注册COM组件后对注册表产生的变动
    vc 字符串与指针
    SQL Server不允许进行远程连接的解决办法
    vc上字符串,CString ,string,char数组&char指针
    如何用Visual C#来创建、修改注册信息
  • 原文地址:https://www.cnblogs.com/longronglang/p/11955111.html
Copyright © 2011-2022 走看看