Mockito 使用
阅读原文时间:2023年07月11日阅读:1

1. 算术测试类

package com.smart.test.mockito;

public interface Calculator {
public int add(int a, int b);
}

package com.smart.test.mockito;

public class CalculatorImpl implements Calculator {

 @Override  
 public int add(int a, int b) {  
     System.out.println("add(" + a + ", " + b + ") = " + (a + b));  
     return a + b;  
 }

 public int minus(int a, int b) {  
     System.out.println("minus(" + a + ", " + b + ") = " + (a - b));  
     return a - b;  
 }  

}

2. 用户测试类

package com.smart.test.mockito;

public class User {

 private String name;

 private String password;

 public User() {}

 public User(String name, String password) {  
     this.name = name;  
     this.password = password;  
 }

 public String getName() {  
     return name;  
 }

 public void setName(String name) {  
     this.name = name;  
 }

 public String getPassword() {  
     return password;  
 }

 public void setPassword(String password) {  
     this.password = password;  
 }  

}

package com.smart.test.mockito;

public interface UserService {

 public User findUserByUserName(String name);

 public boolean hasMatchUser(String name, String password);

 public void registerUser(User user);  

}

package com.smart.test.mockito;

public class UserServiceImpl implements UserService {

 @Override  
 public User findUserByUserName(String name) {  
     // TODO Auto-generated method stub  
     return null;  
 }

 @Override  
 public boolean hasMatchUser(String name, String password) {  
     // TODO Auto-generated method stub  
     return false;  
 }

 @Override  
 public void registerUser(User user) {  
     // TODO Auto-generated method stub

 }  

}

3. 测试驱动类

package com.smart.test.mockito;

import static org.mockito.Mockito.*;
import static junit.framework.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class MockitoSampleTest {

 CalculatorImpl mockCalculatorImpl = mock(CalculatorImpl.class);

 @Mock  
 UserService mockUserService;  
 @Mock  
 UserServiceImpl mockUserServiceImpl;  
 @Mock  
 User mockUser;

 @Before  
 public void initMocks() {  
     /\*  
      \*  初始化当前测试类的所有@Mock注解模拟对象  
      \*/  
     MockitoAnnotations.initMocks(this);  
 }

 @Test  
 public void testMockCalculator() {

     when(mockCalculatorImpl.add(3, 4)).thenReturn(6);  
     int c = mockCalculatorImpl.add(3, 4);  
     assertEquals(c, 7);

     doReturn(2).when(mockCalculatorImpl).minus(3, 1);  
     int d = mockCalculatorImpl.minus(3, 1);  
     assertEquals(d, 2);  
 }

 @Test  
 public void testMockUserService() {  
     /\*  
      \*  对 public User findUserByUserName(String name) 方法设定返回值  
      \*  模拟入参“tom”, 返回一个new User("tom", "1234")对象  
      \*/  
     when(mockUserService.findUserByUserName("tom")).thenReturn(new User("tom", "1234"));

     /\*  
      \*  对 public boolean hasMatchUser(String name, String password) 方法设定返回值  
      \*  模拟入参hasMatchUser("asn", "1234") 返回 true  
      \*/  
     doReturn(true).when(mockUserService).hasMatchUser("asn", "1234");

     /\*  
      \*  对 public void registerUser(User user) 方法设定返回值  
      \*  模拟入参 new User("John", "1234")能够顺利执行,返回值类型为void  
      \*/  
     doNothing().when(mockUserService).registerUser(new User("John", "1234"));

     //调用测试目标方法  
     User user = mockUserService.findUserByUserName("tom");  
     boolean isMatch = mockUserService.hasMatchUser("asn", "1234");  
     mockUserService.registerUser(new User("John", "1234"));

     //断言结果  
     System.out.println(user.getName() + ", " + user.getPassword());  
     System.out.println(isMatch);  
 }

 @Test  
 public void testMockUser() {

     /\*  
      \* 对。。。方法进行模拟测试  
      \*     public String getName() {  
             return name;  
         }  
      \* 设定模拟对象mockUser上getName()方法的返回值为“tom”  
      \*/  
     when(mockUser.getName()).thenReturn("tom");

     assertEquals(mockUser.getName(), "tom");  
 }  

}