PowerMock学习(三)之Mock局部变量

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

PowerMock学习(三)之Mock局部变量

久曲健   2019-11-20 我要评论

编写powermock用例步骤:

  • 类上面先写这两个注解@RunWith(PowerMockRunner.class)、@PrepareForTest(StudentService.class)
  • 先模拟一个假对象即studentdao方法中的局部变量
  • 用无参的方式new对象
  • 再模拟这个对象被调用时,是否有返回,有返回值给出默认值,没有用doNothing()
  • 验证有返回值使用assertEquals即可,无返回值使用Mockito.verify验证

实际案例

接着上一篇文章中的代码,修改下service中的代码,这次我不通过构造器注入Dao,在方法中new一个StudentDao,创建一个名为StudentNewService的类。

具体示例代码如下:

package com.rongrong.powermock.service;

import com.rongrong.powermock.dao.StudentDao;

/**
 * @author rongrong
 * @version 1.0
 * @date 2019/11/17 21:13
 */
public class StudentNewService {


    /**
     * 获取学生个数
     * @return返回学生总数
     */
    public int getTotal() {
        StudentDao studentDao = new StudentDao();
        return studentDao.getTotal();
    }

    /**
     * 创建学生
     * @param student
     */
    public void createStudent(Student student) {
        StudentDao studentDao = new StudentDao();
        studentDao.createStudent(student);
    }
}

针对上面修改部分代码,进行单元测试,以下代码有采用传统方式测试和采用powermock方式进行测试,具体代码如下:

package com.rongrong.powermock.service;

import com.rongrong.powermock.dao.StudentDao;
import org.junit.Assert;
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 org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * @author rongrong
 * @version 1.0
 * @date 2019/11/20 21:42
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest(StudentNewService.class)
public class TestNewStudentService {

    /**
     * 传统方式测试
     */
    @Test
    public void testGetStudentTotal() {
        StudentNewService studentNewService = new StudentNewService();
        int total = studentNewService.getTotal();
        assertEquals(total, 10);
    }

    /**
     * @desc测试有返回值类型 采用powermock进行测试获取学生个数
     */
    @Test
    public void testGetStudentTotalWithPowerMock() {
        //先模拟一个假对象即studentdao方法中的局部变量
        StudentDao studentDao = PowerMockito.mock(StudentDao.class);
        try {
            //这句话我按照英文理解就是,我用无参的方式new了一个StudentDao对象
            PowerMockito.whenNew(StudentDao.class).withNoArguments().thenReturn(studentDao);
            //再模拟这个对象被调用时,我们默认假定返回10个证明调用成功
            PowerMockito.when(studentDao.getTotal()).thenReturn(10);
            //这里就是service就不用再说了
            StudentNewService studentNewService = new StudentNewService();
            int total = studentNewService.getTotal();
            assertEquals(total, 10);
        } catch (Exception e) {
            fail("测试失败了!!!");
            e.printStackTrace();
        }

    }

    /**
     * @desc测试的无返回值类型 采用powermock进行测试创建学生
     */
    @Test
    public void testCreateStudentWithPowerMock() {
        //先模拟一个假对象即studentdao方法中的局部变量
        StudentDao studentDao = PowerMockito.mock(StudentDao.class);
        try {
            //这句话我按照英文理解就是,我用无参的方式new了一个StudentDao对象
            PowerMockito.whenNew(StudentDao.class).withNoArguments().thenReturn(studentDao);
            Student student = new Student();
            //这句话注释与否都能运行通过,也就是我只能判断他是否被调用
            //PowerMockito.doNothing().when(studentDao).createStudent(student);
            //这里就是service就不用再说了
            StudentNewService studentNewService = new StudentNewService();
            studentNewService.createStudent(student);
            Mockito.verify(studentDao).createStudent(student);
        } catch (Exception e) {
            fail("测试失败了!!!");
            e.printStackTrace();
        }

    }

}

运行上面的测试用例,会发现第一个失败,后面两个都运行成功,即有返回值和无返回值类型的测试(void类型)。

 

 

注意:对于无返回值类型的测试,只能验证其是否被调用,这里还请注意。

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们