以前喜欢说废话,废话少说,直接上代码。
一、测试类基类
@RunWith(MockitoJUnitRunner.class)
public abstract class BaseMockTest {
}
二、被测试类
举一个例子,代码如下,不需要看明白代码,只需要知道这是一个controller的类,manager,dao层的mock都如此测试即可。
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@Log4j2
@RestController
public class InvoiceReceiveAPI {
@Autowired
private BlueInvoiceReceiveManager blueInvoiceReceiveManager;
@RequestMapping(value = "/oms_order_blue_invoice_notify", method = RequestMethod.POST)
public InvoiceResponse blueInvoiceNotify(@RequestBody BlueInvoiceCreateDTO blueInvoiceCreateDTO){
InvoiceResponse invoiceResponse = InvoiceResponseUtil.getInvoiceResponse();
try {
blueInvoiceReceiveManager.receiveBlueInvoice(blueInvoiceCreateDTO);
}catch (Exception e){
InvoiceResponseUtil.dealException(invoiceResponse, e, log);
}
return invoiceResponse;
}
}
三、写UT三部曲:given、when、then
这里即给上面代码写mock。
需要mock所有的场景(失败场景 + 成功场景)
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.context.ApplicationContext;
/**
*
* @author Shuai Chen
* 2019年1月10日 下午4:55:11
*
*/
public class HubNotifyInvoiceAPIMockTest extends BaseMockTest{
//进入类内部
@InjectMocks
private HubNotifyInvoiceAPI hubNotifyInvoiceAPI;
//模拟,而不会真正进入类的内部
@Mock
private ApplicationContext applicationContext;
@Mock
private ConfigContactManager configContactManager;
@Test
public void inner_失败() {
//given
String sign = "";
String requestBody = "{\"method\":\"chen\",\"contactCode\":\"da\"}";
//doReturn里边是返回值,when里边是使用的类(一般是manager类),后面跟的是方法,使用它调用方法,方法里边接的是模拟
doReturn(new InvoiceResultAPI()).when(applicationContext).getBean(anyString());
doReturn(null).when(configContactManager).findConfigContactByContactCode(anyString());
//when
String actualStr = hubNotifyInvoiceAPI.inner(sign, requestBody);
//then
verify(applicationContext).getBean(anyString());
verify(configContactManager, never()).findConfigContactByContactCode(anyString());
assertEquals("fail", actualStr);
}
}
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
/**
*
* @author Shuai Chen
* 2019年1月10日 下午4:55:16
*
*/
public class InvoiceReceiveApiMockTest extends BaseMockTest{
@InjectMocks
private InvoiceReceiveAPI invoiceReceiveAPI;
@Mock
private BlueInvoiceReceiveManager blueInvoiceReceiveManager;
@Mock
private RedBlueInvoiceReceiveManager redBlueInvoiceReceiveManager;
@Test
public void 开蓝票推送接口_处理抛IllegalArgumentException() {
//given
BlueInvoiceCreateDTO blueInvoiceCreateDTO = new BlueInvoiceCreateDTO();
//这里模拟抛异常
doThrow(new IllegalArgumentException()).when(blueInvoiceReceiveManager).receiveBlueInvoice(any(BlueInvoiceCreateDTO.class));
//when
InvoiceResponse invoiceResponse = invoiceReceiveAPI.blueInvoiceNotify(blueInvoiceCreateDTO);
//then
//验证方法被调用使用verify,验证值使用assertEquals或assertTrue,assertNotTrue
verify(blueInvoiceReceiveManager).receiveBlueInvoice(any(BlueInvoiceCreateDTO.class));
assertEquals(Constants.DEEL_ARG_FAIL_CODE, invoiceResponse.getErrorCode());
}
@Test
public void 开蓝票推送接口_处理抛BusinessException() {
//given
BlueInvoiceCreateDTO blueInvoiceCreateDTO = new BlueInvoiceCreateDTO();
doThrow(new BusinessException("", "")).when(blueInvoiceReceiveManager).receiveBlueInvoice(any(BlueInvoiceCreateDTO.class));
//when
invoiceReceiveAPI.blueInvoiceNotify(blueInvoiceCreateDTO);
//then
verify(blueInvoiceReceiveManager).receiveBlueInvoice(any(BlueInvoiceCreateDTO.class));
}
@Test
public void 开蓝票推送接口_处理抛Exception() {
//given
BlueInvoiceCreateDTO blueInvoiceCreateDTO = new BlueInvoiceCreateDTO();
doThrow(new RuntimeException()).when(blueInvoiceReceiveManager).receiveBlueInvoice(any(BlueInvoiceCreateDTO.class));
//when
InvoiceResponse invoiceResponse = invoiceReceiveAPI.blueInvoiceNotify(blueInvoiceCreateDTO);
//then
verify(blueInvoiceReceiveManager).receiveBlueInvoice(any(BlueInvoiceCreateDTO.class));
assertEquals(ExceptionEnum.ERROR.getResultCode(), invoiceResponse.getErrorCode());
}
@Test
public void 开蓝票推送接口_成功() {
//given
BlueInvoiceCreateDTO blueInvoiceCreateDTO = new BlueInvoiceCreateDTO();
//when
InvoiceResponse invoiceResponse = invoiceReceiveAPI.blueInvoiceNotify(blueInvoiceCreateDTO);
//then
verify(blueInvoiceReceiveManager).receiveBlueInvoice(any(BlueInvoiceCreateDTO.class));
assertEquals(Constants.SUCCESS, invoiceResponse.getStatus());
}