当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Mockito单元测试实例

发表于: 2013-12-09   作者:bijian1013   来源:转载   浏览:
摘要: Mockito单元测试实例: public class SettingServiceTest { private List<PersonDTO> personList = new ArrayList<PersonDTO>(); @InjectMocks private SettingPojoService settin

Mockito单元测试实例:

public class SettingServiceTest {
    
    private List<PersonDTO> personList = new ArrayList<PersonDTO>();
    
    @InjectMocks
    private SettingPojoService settingPojoService;
    
    @Mock
    private SettingDAO mockSettingDAO;

    @BeforeClass
    public static void prepareData() throws Exception {
    }
    
    @AfterClass
    public static void destoryData() throws Exception {
    }
    
    @Before
    public void setUp() throws Exception {
        
        MockitoAnnotations.initMocks(this); 
        
        PersonDTO personDTO1 = new PersonDTO();
        PersonDTO personDTO2 = new PersonDTO();
        PersonDTO personDTO3 = new PersonDTO();

        personDTO1.setDeptCode("BCDEF");
        personDTO1.setHanderCode("BIJIAN001");

        personDTO2.setDeptCode("HIJKL");
        personDTO2.setHanderCode("BIJIAN002");

        personDTO3.setDeptCode("OPQRS");
        personDTO3.setHanderCode("BIJIAN003");

        personList.add(personDTO1);
        personList.add(personDTO2);
        personList.add(personDTO3);
    }
    
    @After
    public void tearDown() throws Exception {
    }
    
    @Test
    public void test_queryHandlerCode_by_mock() {

        String departmentCode = "ABC";
        try {
            when(mockSettingDAO.queryHandlerCode(departmentCode)).thenReturn("BIJIAN");
            String code = settingPojoService.queryHandlerCode(departmentCode);
            verify(mockSettingDAO, times(1)).queryHandlerCode(departmentCode);
            Assert.assertEquals("BIJIAN", code);
        } catch (DAOException e) {
            Assert.fail(e.getLocalizedMessage());
        } catch (BusinessServiceException e) {
            Assert.fail(e.getLocalizedMessage());
        }
    }

    @Test
    public void test_savePerson_noral_list_by_mock() {
        
        try {
            settingPojoService.savePerson(personList);
            verify(mockSettingDAO).savePerson(personList.get(0));
            verify(mockSettingDAO).savePerson(personList.get(1));
            verify(mockSettingDAO).savePerson(personList.get(2));
            verify(mockSettingDAO, times(1)).savePerson(personList.get(0));
            verify(mockSettingDAO, times(1)).savePerson(personList.get(1));
            verify(mockSettingDAO, times(1)).savePerson(personList.get(2));
        } catch (DAOException e) {
            Assert.fail(e.getLocalizedMessage());
        } catch (BusinessServiceException e) {
            Assert.fail(e.getLocalizedMessage());
        }
    }
    
    @Test
    public void test_savePerson_null_list_by_mock() {

        PersonDTO personDTO1 = personList.get(0);
        personList = null;
        try {
            settingPojoService.savePerson(personList);
            verify(mockSettingDAO, times(0)).savePerson(personDTO1);
            verifyZeroInteractions(mockSettingDAO);
            verifyNoMoreInteractions(mockSettingDAO);
        } catch (PafaDAOException e) {
            Assert.fail(e.getLocalizedMessage());
        } catch (BusinessServiceException e) {
            Assert.fail(e.getLocalizedMessage());
        } 
    }
    
    @Test
    public void test_savePerson_list_only_contain_1_object_by_mock() {
        
        personList.remove(2);
        PersonDTO personDTO1 = personList.remove(1);
        try {
            settingPojoService.savePerson(personList);
            verify(mockSettingDAO, times(1)).savePerson(personList.get(0));
            verify(mockSettingDAO, times(0)).savePerson(personDTO1);
            verify(mockSettingDAO, never()).savePerson(personDTO1);
        } catch (DAOException e) {
            Assert.fail(e.getLocalizedMessage());
        } catch (BusinessServiceException e) {
            Assert.fail(e.getLocalizedMessage());
        } 
    }
    
    @Test
    public void test_deleteHandler_noral_list_by_mock() {
        
        try {
            settingPojoService.deleteHandler(personList);
            verify(mockSettingDAO).deleteHandler(personList.get(0));
            verify(mockSettingDAO).deleteHandler(personList.get(1));
            verify(mockSettingDAO).deleteHandler(personList.get(2));
            verify(mockSettingDAO, times(1)).deleteHandler(personList.get(0));
            verify(mockSettingDAO, times(1)).deleteHandler(personList.get(1));
            verify(mockSettingDAO, times(1)).deleteHandler(personList.get(2));
        } catch (DAOException e) {
            Assert.fail(e.getLocalizedMessage());
        } catch (BusinessServiceException e) {
            Assert.fail(e.getLocalizedMessage());
        }
    }
    
    @Test
    public void test_deleteHandler_null_list_by_mock() {

        PersonDTO personDTO1 = personList.get(0);
        personList = null;
        
        try {
            settingPojoService.deleteHandler(personList);
            verify(mockSettingDAO, times(0)).deleteHandler(personDTO1);
            verifyZeroInteractions(mockSettingDAO);
            verifyNoMoreInteractions(mockSettingDAO);
        } catch (DAOException e) {
            Assert.fail(e.getLocalizedMessage());
        } catch (BusinessServiceException e) {
            Assert.fail(e.getLocalizedMessage());
        } 
    }
    
    @Test
    public void test_deleteHandler_list_only_contain_1_object_by_mock() {
        
        personList.remove(2);
        PersonDTO personDTO1 = personList.remove(1);
        
        try {
            settingPojoService.deleteHandler(personList);
            verify(mockSettingDAO, times(1)).deleteHandler(personList.get(0));
            verify(mockSettingDAO, times(0)).deleteHandler(personDTO1);
            verify(mockSettingDAO, never()).deleteHandler(personDTO1);
        } catch (DAOException e) {
            Assert.fail(e.getLocalizedMessage());
        } catch (BusinessServiceException e) {
            Assert.fail(e.getLocalizedMessage());
        } 
    }
    
    @Test
    public void test_isDepartmentHandler_by_mock() {
        
        String code = "BIJIAN001";
        String departmentCode = "ABC";
        try {
            when(mockSettingDAO.isDepartmentHandler(code, departmentCode)).thenReturn(true);
            boolean res = settingPojoService.isDepartmentHandler(code, departmentCode);
            verify(mockSettingDAO, times(1)).isDepartmentHandler(code, departmentCode);
            Assert.assertEquals(true, res);
        } catch (DAOException e) {
            Assert.fail(e.getLocalizedMessage());
        } catch (BusinessServiceException e) {
            Assert.fail(e.getLocalizedMessage());
        } 
    }
}

 

Mockito单元测试实例

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
前言 要做单元测试,注定少不了单元测试框架和Mock框架.本文选择Junit +Mockito来讨论。先解释一下
1. 为什么使用Mockito来进行单元测试? 回答这个问题需要回答两个方面,第一个是为什么使用mock?mo
引言 Hadoop MapReduce作业有着独一无二的代码架构,这种代码架构拥有特定的模板和结构。这样的架构
转自:http://yuanzhifei89.iteye.com/blog/1122104 作者:yuanzhifei 测试相关资源 让开发自动化:
项目文件结构图: 椭圆框中的Jar 包是单元测试时候需要引入的。 矩形框 MainTest 每个包下一个,为
一、测试准备工作:NUnit的下载及安装 下载:http://download.csdn.net/detail/wangqingbo0829/8470
为什么需要Mock 测试驱动的开发(Test Driven Design, TDD)要求我们先写单元测试,再写实现代码。在
本文主要介绍了如何使用 JUnit 4 提供的各种功能开展有效的单元测试,并通过一个实例演示了如何使用
11-3URLTestDemo 1、File -> New -> Project 在左边模板中选择Visual C#里的Web,对应到的项
JUnit基础及第一个单元测试实例(JUnit3.8) 单元测试   单元测试(unit testing) ,是指对软件
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号