1. 算术测试类
1 package com.smart.test.mockito; 2 3 public interface Calculator { 4 public int add(int a, int b); 5 }
1 package com.smart.test.mockito; 2 3 public class CalculatorImpl implements Calculator { 4 5 @Override 6 public int add(int a, int b) { 7 System.out.println("add(" + a + ", " + b + ") = " + (a + b)); 8 return a + b; 9 } 10 11 public int minus(int a, int b) { 12 System.out.println("minus(" + a + ", " + b + ") = " + (a - b)); 13 return a - b; 14 } 15 }
2. 用户测试类
1 package com.smart.test.mockito; 2 3 public class User { 4 5 private String name; 6 7 private String password; 8 9 public User() {} 10 11 public User(String name, String password) { 12 this.name = name; 13 this.password = password; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public String getPassword() { 25 return password; 26 } 27 28 public void setPassword(String password) { 29 this.password = password; 30 } 31 }
1 package com.smart.test.mockito; 2 3 public interface UserService { 4 5 public User findUserByUserName(String name); 6 7 public boolean hasMatchUser(String name, String password); 8 9 public void registerUser(User user); 10 }
1 package com.smart.test.mockito; 2 3 public class UserServiceImpl implements UserService { 4 5 @Override 6 public User findUserByUserName(String name) { 7 // TODO Auto-generated method stub 8 return null; 9 } 10 11 @Override 12 public boolean hasMatchUser(String name, String password) { 13 // TODO Auto-generated method stub 14 return false; 15 } 16 17 @Override 18 public void registerUser(User user) { 19 // TODO Auto-generated method stub 20 21 } 22 }
3. 测试驱动类
1 package com.smart.test.mockito; 2 3 import static org.mockito.Mockito.*; 4 import static junit.framework.Assert.*; 5 import org.junit.Before; 6 import org.junit.Test; 7 import org.mockito.Mock; 8 import org.mockito.MockitoAnnotations; 9 10 public class MockitoSampleTest { 11 12 CalculatorImpl mockCalculatorImpl = mock(CalculatorImpl.class); 13 14 @Mock 15 UserService mockUserService; 16 @Mock 17 UserServiceImpl mockUserServiceImpl; 18 @Mock 19 User mockUser; 20 21 @Before 22 public void initMocks() { 23 /* 24 * 初始化当前测试类的所有@Mock注解模拟对象 25 */ 26 MockitoAnnotations.initMocks(this); 27 } 28 29 @Test 30 public void testMockCalculator() { 31 32 when(mockCalculatorImpl.add(3, 4)).thenReturn(6); 33 int c = mockCalculatorImpl.add(3, 4); 34 assertEquals(c, 7); 35 36 doReturn(2).when(mockCalculatorImpl).minus(3, 1); 37 int d = mockCalculatorImpl.minus(3, 1); 38 assertEquals(d, 2); 39 } 40 41 @Test 42 public void testMockUserService() { 43 /* 44 * 对 public User findUserByUserName(String name) 方法设定返回值 45 * 模拟入参“tom”, 返回一个new User("tom", "1234")对象 46 */ 47 when(mockUserService.findUserByUserName("tom")).thenReturn(new User("tom", "1234")); 48 49 /* 50 * 对 public boolean hasMatchUser(String name, String password) 方法设定返回值 51 * 模拟入参hasMatchUser("asn", "1234") 返回 true 52 */ 53 doReturn(true).when(mockUserService).hasMatchUser("asn", "1234"); 54 55 /* 56 * 对 public void registerUser(User user) 方法设定返回值 57 * 模拟入参 new User("John", "1234")能够顺利执行,返回值类型为void 58 */ 59 doNothing().when(mockUserService).registerUser(new User("John", "1234")); 60 61 //调用测试目标方法 62 User user = mockUserService.findUserByUserName("tom"); 63 boolean isMatch = mockUserService.hasMatchUser("asn", "1234"); 64 mockUserService.registerUser(new User("John", "1234")); 65 66 //断言结果 67 System.out.println(user.getName() + ", " + user.getPassword()); 68 System.out.println(isMatch); 69 } 70 71 72 @Test 73 public void testMockUser() { 74 75 /* 76 * 对。。。方法进行模拟测试 77 * public String getName() { 78 return name; 79 } 80 * 设定模拟对象mockUser上getName()方法的返回值为“tom” 81 */ 82 when(mockUser.getName()).thenReturn("tom"); 83 84 assertEquals(mockUser.getName(), "tom"); 85 } 86 }