09.Mockito常用方法

本节主要说明Mockito的常用方法。

Mockito的验证行为

//Mock一个List对象
List mock = mock(List.class);
//使用mock的对象
mock.add("Hello");
//验证行为是否发生
verify(mock).add("Hello");

模拟方法调用结果

package com.cloud.app.service;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Date;

import org.cloud.app.entity.User;
import org.cloud.app.rpc.UserRpc;
import org.cloud.app.service.UserService;
import org.junit.Before;
import org.junit.Test;

public class UserServiceTest {

	private UserService userService;

	@Before
	public void before() {
		userService = new UserService();
		UserRpc userRpc = mock(UserRpc.class);
		User user1 = new User();
		user1.setUserName("user1");
		user1.setRealName("real1");
		user1.setBirthday(new Date());
		
		User user2 = new User();
		user2.setUserName("user2");
		user2.setRealName("real2");
		user2.setBirthday(new Date());
		when(userRpc.getUser(1)).thenReturn(user1).thenReturn(user2);
		userService.setUserRpc(userRpc);
	}

	@Test
	public void testGetUser() {
		User user1 = userService.getUser(1);
		assertEquals(user1.getUserName(), "user1");
		assertEquals(user1.getRealName(), "real1");
		User user2 = userService.getUser(1);
		assertEquals(user2.getUserName(), "user2");
		assertEquals(user2.getRealName(), "real2");
		User user3 = userService.getUser(1);
		assertEquals(user3.getUserName(), "user2");
		assertEquals(user3.getRealName(), "real2");	
	}

}

上面的示例中,在Before方法中,when(userRpc.getUser(1)).thenReturn(user1).thenReturn(user2);为Mock添加了两次返回,在testGetUser中,调用userService.getUser(1),三次,第一次返回User1,第二次返回User2,第三次依然返回User2。

抛出异常

package com.cloud.app.service;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.cloud.app.entity.User;
import org.cloud.app.rpc.UserRpc;
import org.cloud.app.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserServiceTest {

	private Logger log = LoggerFactory.getLogger(UserServiceTest.class);

	private UserService userService;

	@Before
	public void beforeClass() {
		userService = new UserService();
		UserRpc userRpc = mock(UserRpc.class);
		when(userRpc.getUser(1)).thenThrow(new RuntimeException("异常"));
		//doThrow(new RuntimeException("异常")).when(userRpc).getUser(1); //另一种写法
		userService.setUserRpc(userRpc);
	}

	@Test
	public void testGetUser() {
		User user1 = userService.getUser(1);
		log.info(user1.toString());
	}
}

重置mock

package com.cloud.app.service;

import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;

import org.cloud.app.entity.User;
import org.cloud.app.rpc.UserRpc;
import org.cloud.app.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserServiceTest {

	private Logger log = LoggerFactory.getLogger(UserServiceTest.class);

	private UserService userService;

	@Mock
	private UserRpc userRpc;
	
	
	@Before
	public void before() {
		userService = new UserService();
		
		MockitoAnnotations.initMocks(this);
		//when(userRpc.getUser(1)).thenThrow(new RuntimeException("异常"));
		doThrow(new RuntimeException("异常")).when(userRpc).getUser(1);
		
		userService.setUserRpc(userRpc);
	}

	@Test
	public void testGetUser() {
	
		reset(userRpc);
		when(userRpc.getUser(1)).thenReturn(new User());
		User user1 = userService.getUser(1);
		log.info(user1.toString());
	}
}

reset(userRpc);方法重置了userRpc的所有Mock。

使用注解完成Mock

package com.cloud.app.service;

import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.cloud.app.entity.User;
import org.cloud.app.rpc.UserRpc;
import org.cloud.app.service.UserService;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserServiceTest {

	private Logger log = LoggerFactory.getLogger(UserServiceTest.class);

	private UserService userService;

	@Mock
	private UserRpc userRpc;
	
	
	@Before
	public void before() {
		userService = new UserService();
		
		MockitoAnnotations.initMocks(this);
		//when(userRpc.getUser(1)).thenThrow(new RuntimeException("异常"));
		doThrow(new RuntimeException("异常")).when(userRpc).getUser(1);
		
		userService.setUserRpc(userRpc);
	}

	@Test
	public void testGetUser() {
		User user1 = userService.getUser(1);
		log.info(user1.toString());
	}
}

在示例中,使用@Mock注释了变量 userRpc , 在before方法中MockitoAnnotations.initMocks(this);会使用Mock初始化这个变量。