PowerMock learning (d) the use of Mock static

When we write code, there will always write some tools, in order to facilitate the call prefer to use the static keyword to modify the corresponding method.

So now illustrate, or prepare two interfaces, the first query is the total number of students, the second is the new student two interfaces, specific examples of code is as follows:

Package com.rongrong.powermock.mockstatic; 

Import com.rongrong.powermock.service.Student; 

/ ** 
 * @author Rongrong 
 * @version 1.0 
 * @date 2019/11/23 8:08 
 * / 
public  class StudentStaticService { 

    / * * 
     * get the total number of students 
     * @return 
     * / 
    public  int getStudentTotal () {
         return StudentUtils.getStudent (); 
    } 

    / ** 
     * create a student 
     * @param student
      * / 
    public  void createStudent (student student) {
        StudentUtils.createStudent(student);
    }
}

Then we look at this static utility class StudentUtils, the specific code examples are as follows:

Package com.rongrong.powermock.mockstatic; 

Import com.rongrong.powermock.service.Student; 

/ ** 
 * @author Rongrong 
 * @version 1.0 
 * @date 2019/11/23 7:38 
 * / 
public  class StudentUtils {
     / * * 
     * get the total number of students 
     * @return 
     * / 
    public  static  int getStudent () {
         the throw  new new UnsupportedOperationException (); 
    } 

    / ** 
     * create a student 
     * @param student
      * / 
    public  static void createStudent(Student student){
        throw new UnsupportedOperationException();
    }
}

Next, we use the conventional manner to do unit testing, the following sample code:

    @Test
    public void testGetStudnetTotal(){
        StudentStaticService staticService = new StudentStaticService();
        int studentTotal = staticService.getStudentTotal();
        assertEquals(studentTotal,10);
    }

    @Test
    public void testCreateStudent(){
        StudentStaticService staticService = new StudentStaticService();
        staticService.createStudent(new Student());
        assertTrue(true);
    }

Then run the test case, the results confirmed the error, why error, will not elaborate here, before refer to the article, an error, as shown below:

 

Next we used to test powermock, specifically the following sample code:

 @Test
    public void testGetStudentWithMock(){
        //先mock工具类对象
        PowerMockito.mockStatic(StudentUtils.class);
        //模拟静态类调用
        PowerMockito.when(StudentUtils.getStudent()).thenReturn(10);
        //构建service
        StudentStaticService service = new StudentStaticService();
        int studentTotal = service.getStudentTotal();
        assertEquals(10,studentTotal);
    }

    @Test
    public void testCreateStudentWithMock(){
        //先模拟静态工具类
        PowerMockito.mockStatic(StudentUtils.class);
        //模拟调用
        PowerMockito.doNothing().when(StudentUtils.class);
        //构建service
        StudentStaticService service = new StudentStaticService();
        Student student = new Student();
        service.createStudent(student);
        //这里用powermock来验证,而不是mock,更体现了powermock的强大
        PowerMockito.verifyStatic();
    }

再次运行,测试通过,如下图所示:

 

 

运行之前先让powermock为我们准备了StudentUtils工具类,而且采用mockstatic的方法,最后我们用powermock.verifyStatic()验证,而不是mock,更体现了powermock的强大。

 

Guess you like

Origin www.cnblogs.com/longronglang/p/11915449.html