Mockito单元测试实例
编程技术  /  houtizong 发布于 3年前   106
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()); } }}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接