欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mockito单元测试实例

程序员文章站 2022-07-13 10:37:10
...

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());
        } 
    }
}