本文主要是介绍junit5+mockito 实现对私有方法进行模拟和测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
需引入依赖:
<dependency><groupId>org.powermock</groupId><artifactId>powermock-api-mockito2</artifactId><version>2.0.9</version><scope>test</scope></dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-module-junit4</artifactId><version>2.0.9</version><scope>test</scope></dependency>
package com.zdsoft.dataprepare.service.impl;import com.zdsoft.dataprepare.dto.CheckDataVO;
import com.zdsoft.dataprepare.entity.DataCheckRecord;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;import static org.junit.Assert.assertEquals;@RunWith(PowerMockRunner.class)
@PrepareForTest(value = {DataCheckRecordServiceImpl.class})
public class DataCheckRecordServiceImplTest {/*** 验证的dcsResponse中的resolveResultState为0时 其余均为好的* @return state为11 ; stateDesc 为:"状态解释:财税数据授权失败,需重新授权,模型返回信息或异常信息:发票数据状态接口轮询准备数据达到最高次数终止任务,输入:{\"sourceType\":\"05\",\"creditTraceNo\":\"20230828001\",\"type\":\"M04\"},输出:{\"data\":{\"data\":\"{\\\"orderId\\\":\\\"S204909230007\\\",\\\"resolveResultState\\\":\\\"0\\\",\\\"taxNo\\\":\\\"TAX_NO_0120230918164844793\\\",\\\"taxpayerId\\\":\\\"91500000MA5UTMTB6F\\\",\\\"userFeedbackState\\\":\\\"2\\\"}\",\"subCode\":\"SUCCESS\",\"errCode\":\"000000\",\"errMsg\":\"交易成功\",\"rtnMsg\":\"服务调用成功\",\"bizSuccess\":true,\"rtnCode\":\"SUCCESS\"}}"* @return dcsResponse.getMsg信息*/@Testpublic void testCheckEntVoiceStatus() throws Exception {// 创建待验证的对象CheckDataVO checkDataVO = new CheckDataVO();checkDataVO.setState(11);String stateDesc = "状态解释:财税数据授权失败,需重新授权,模型返回信息或异常信息:发票数据状态接口轮询准备数据达到最高次数终止任务,输入:{\"sourceType\":\"05\",\"creditTraceNo\":\"20230828001\",\"type\":\"M04\"},输出:{\"data\":{\"data\":\"{\\\"orderId\\\":\\\"S204909230007\\\",\\\"resolveResultState\\\":\\\"0\\\",\\\"taxNo\\\":\\\"TAX_NO_0120230918164844793\\\",\\\"taxpayerId\\\":\\\"91500000MA5UTMTB6F\\\",\\\"userFeedbackState\\\":\\\"2\\\"}\",\"subCode\":\"SUCCESS\",\"errCode\":\"000000\",\"errMsg\":\"交易成功\",\"rtnMsg\":\"服务调用成功\",\"bizSuccess\":true,\"rtnCode\":\"SUCCESS\"}}";checkDataVO.setStateDesc(stateDesc);DataCheckRecord checkRecord = new DataCheckRecord();checkRecord.setCheckState(9);checkRecord.setCheckSerialNumber("SN202403061002249166HSJV02");checkRecord.setDecResponse("{\"code\":500,\"data\":null,\"msg\":\"发票数据状态接口轮询准备数据达到最高次数终止任务,输入:{\\\"sourceType\\\":\\\"05\\\",\\\"creditTraceNo\\\":\\\"20230828001\\\",\\\"type\\\":\\\"M04\\\"},输出:{\\\"data\\\":{\\\"data\\\":\\\"{\\\\\\\"orderId\\\\\\\":\\\\\\\"S204909230007\\\\\\\",\\\\\\\"resolveResultState\\\\\\\":\\\\\\\"0\\\\\\\",\\\\\\\"taxNo\\\\\\\":\\\\\\\"TAX_NO_0120230918164844793\\\\\\\",\\\\\\\"taxpayerId\\\\\\\":\\\\\\\"91500000MA5UTMTB6F\\\\\\\",\\\\\\\"userFeedbackState\\\\\\\":\\\\\\\"2\\\\\\\"}\\\",\\\"subCode\\\":\\\"SUCCESS\\\",\\\"errCode\\\":\\\"000000\\\",\\\"errMsg\\\":\\\"交易成功\\\",\\\"rtnMsg\\\":\\\"服务调用成功\\\",\\\"bizSuccess\\\":true,\\\"rtnCode\\\":\\\"SUCCESS\\\"}}\"}");String serialNumber = "SN202403061002249166HSJV02";CheckDataVO checkDataVO1 = Whitebox.invokeMethod(DataCheckRecordServiceImpl.class, "checkEntVoiceStatus", checkRecord, serialNumber);Assertions.assertAll(() -> {assertEquals(checkDataVO.getState(), checkDataVO1.getState());assertEquals(checkDataVO.getStateDesc(), checkDataVO1.getStateDesc());});}}
这篇关于junit5+mockito 实现对私有方法进行模拟和测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!