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

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

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号