Mock Final
mockfinal relatively speaking, is relatively simple, using powermock to test the use of final modified method or class, relatively simple, call the part of the interface, or service call dao.
For the interface and the scene will not elaborate here, it is particularly simple.
service layer
Specific code examples are as follows:
package com.rongrong.powermock.mockfinal; /** * @author rongrong * @version 1.0 * @date 2019/11/27 21:29 */ public class StudentFinalService { private StudentFinalDao studentFinalDao; public StudentFinalService(StudentFinalDao studentFinalDao) { this.studentFinalDao = studentFinalDao; } public void createStudent(Student student) { studentFinalDao.isInsert(student); } }
dao layer
In order to simulate the test, I added the class dao layer of a final keyword modification, this class is not allowed to be inherited.
Specific code as follows:
package com.rongrong.powermock.mockfinal; /** * @author rongrong * @version 1.0 * @date 2019/11/27 21:20 * / Final public class StudentFinalDao { public Boolean isInsert(Student student){ throw new UnsupportedOperationException(); } }
Unit testing
In order to distinguish the difference between powermock and Easymock, let's adopt EasyMock test, where the first EasyMock ignore usage, interested students can learn on their own to try.
Use EasyMock test
Specific code examples are as follows:
@Test public void testStudentFinalServiceWithEasyMock(){ //mock对象 StudentFinalDao studentFinalDao = EasyMock.createMock(StudentFinalDao.class); Student Student = new new Student (); // mock call, the default return success EasyMock.expect (studentFinalDao.isInsert (Student)) andReturn (. To true ); EasyMock.replay(studentFinalDao); StudentFinalService studentFinalService = new StudentFinalService(studentFinalDao); studentFinalService.createStudent(student); EasyMock.verify(studentFinalDao); }
Let's run this unit test, you will find running error, specifically as shown below:
Obviously due to the modification of the final keyword, leading to not let the test is successful, we can remove the final crucial test again and found that test.
Use PowerMock test
Specific code examples are as follows:
package com.rongrong.powermock.mockfinal; 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; /** * @author rongrong * @version 1.0 * @date 2019/11/27 22:10 */ @RunWith(PowerMockRunner.class) @PrepareForTest(StudentFinalDao.class) public class TestStudentFinalService { @Test public void testStudentFinalServiceWithPowerMock(){ StudentFinalDao studentFinalDao = PowerMockito.mock(StudentFinalDao.class); Student student = new Student(); PowerMockito.when(studentFinalDao.isInsert(student)).thenReturn(true); StudentFinalService studentFinalService = new StudentFinalService(studentFinalDao); studentFinalService.createStudent(student); Mockito.verify(studentFinalDao).isInsert(student); } }
运行上面的单元测试时,会发现运行通过!!