赞
踩
@PrepareForTest //测试类测试原始类所需的静态类的声明
语法:@PrepareForTest(value = {A.class, B.class})
@InjectMocks //测试类的注入注解,相当于Sping的@Autowired,
可以理解成new一个对象
@Mocks //模拟一个虚拟的对象,当测试类不走dao逻辑,可以在测试类中模拟一个虚拟的dao,进而测试原有代码的业务逻辑
@BeforeMethod //测试类执行之前执行
@AfterMethod //测试类运行之后执行
@Test //测试方法
场景1:被测类中不使用new创建对象,如用getInstance()
解决方案:
首先在测试类中声明静态类对象@PrepareForTest
在测试类执行前初始化时,先mock一个虚拟的对象
当调用getInstance方法时,就使用@Mock注入的虚拟对象
这样调用到被测类中的静态类时,将会使用你模拟的那个虚拟类(既thenReturn的对象)
场景2:mock一些工具类的静态方法
解决方案:
场景3:当被测类中的方法内出现new对象的存在时,如何虚拟掉
5. 先创建一个虚拟的对象@Mock
2.测试方法执行前首先执行一行代码(代码意思是,当被测类方法内出现new某个对象时,使用步骤1被虚拟出来的对象)
场景四:方法讲解
场景五:若dao中的方法没有返回类型
若mock的dao中的某个方法没有返回的参数,那么这个方法将不用mock
注意:
案列:
/** * @Author: yyk * @Date: Created in 2021/3/18 17:29 * @Description: 单元测试 * @Modified By: * @Version: 1.0$ * @Demind And Code: */ @PrepareForTest(value = {ArrearageAgeFinalDao.class, EsopUserManagerRelaDao.class, PermissionsMgmt.class, AcctEsopCommonMgt.class, DecimalPointLeaveUtil.class}) public class QueryArrearageAgeServiceTest extends Template { @InjectMocks private QueryArrearageAgeService queryArrearageAgeService; @Mock private ArrearageAgeFinalDao arrearageAgeFinalDaoMock; @Mock private EsopUserManagerRelaDao esopUserManagerRelaDaoMock; @BeforeMethod public void setUp() throws Exception { MockitoAnnotations.initMocks(this); } @Test public void testCallService() throws Exception { //模拟掉对应的dao initPoverMockQueryArrearageAgeDao(); queryArrearageAgeService.callService(initMessage()); } /**初始化请求消息 * @return */ private Message initMessage() { Map<String, Object> reqMap = new HashMap(); reqMap.put("accept_id", "200833491133"); reqMap.put("request_source", "20"); reqMap.put("request_time", "20210305143816"); reqMap.put("operator_id", "jiangwk"); Map<String, Object> orderContent = new HashMap(); orderContent.put("order_type", "1700036"); reqMap.put("order_content", orderContent); Map<String, Object> orderReq = new HashMap(); orderReq.put("accountId", "1000122606"); orderReq.put("customerId", "1000122606"); orderReq.put("billMonthStart", "202101"); orderReq.put("billMonthEnd", "202103"); orderReq.put("operationType", "1"); orderContent.put("order_req", orderReq); Message msg = new Message(reqMap); return msg; } /**模拟掉dao调用的方法 */ private void initPoverMockQueryArrearageAgeDao() { // 模拟掉静态创建的dao PowerMockito.mockStatic(PermissionsMgmt.class); PowerMockito.when(PermissionsMgmt.getJkrOrgZc(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(initStrings()); PowerMockito.mockStatic(ArrearageAgeFinalDao.class); PowerMockito.when(ArrearageAgeFinalDao.getInstance()).thenReturn(arrearageAgeFinalDaoMock); PowerMockito.mockStatic(EsopUserManagerRelaDao.class); PowerMockito.when(EsopUserManagerRelaDao.newInstance()).thenReturn(esopUserManagerRelaDaoMock); PowerMockito.mockStatic(AcctEsopCommonMgt.class); PowerMockito.when(AcctEsopCommonMgt.acctEsopCommon(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(initMap()); PowerMockito.mockStatic(DecimalPointLeaveUtil.class); PowerMockito.when(DecimalPointLeaveUtil.getFloor(Mockito.anyString())).thenReturn("0.00"); // 模拟掉方法 PowerMockito.when(arrearageAgeFinalDaoMock.queryAcctArrearagePage(Mockito.anyMap(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyString())).thenReturn(initPageAcct()); PowerMockito.when(arrearageAgeFinalDaoMock.queryAcctArrearageAge(Mockito.anyMap(), Mockito.anyString())).thenReturn(initAgeBeanList()); PowerMockito.when(esopUserManagerRelaDaoMock.checkPermissions(Mockito.anyString(), Mockito.anyString())).thenReturn(true); } /**初始化initMap * @return */ private Map<String, String> initMap() { Map map = new HashMap<String, String>(); map.put("customerName", "testCustomerName"); return map; } /**初始化String集合 * @return */ private List<String> initStrings() { List<String> stringList = new ArrayList<String>(); stringList.add("0000"); return stringList; } /**模拟ageBean对象 * @return */ private List initAgeBeanList() { List<AcctArrearagrBean> ageBeanList = new ArrayList<>(); AcctArrearagrBean acctArrearagrBean = new AcctArrearagrBean(); acctArrearagrBean.setCustomerName("testCustomerName"); ageBeanList.add(acctArrearagrBean); return ageBeanList; } /** * 模拟acct类 * @return */ private Page initPageAcct() { Page<AcctArrearagrBean> page = new Page<AcctArrearagrBean>(); return page; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。