【单元测试】PowerMock
PowerMock:https://www.ibm.com/developerworks/cn/java/j-lo-powermock/index.html
api文档:https://www.javadoc.io/doc/org.powermock
验证静态void方法,并捕货静态void方法返回参数
public static void deleteSftpFile(String remotePath, String[] filesName, String sftpIp, String sftpUser, String sftpPwd, String sftpPort) throws Exception
@RunWith(PowerMockRunner.class) @PrepareForTest({SftpUtils.class, DateUtils.class}) @PowerMockRunnerDelegate(SpringRunner.class) @PowerMockIgnore({"javax.management.*", "javax.net.ssl.*", "javax.security.*"}) public class JudgeForceFetchStepTest implements ApplicationContextAware { private ApplicationContext applicationContext; @DisplayName("正则表达式账单历史测试") @Test @Order(1) public void testJudeForceFetchBillByRegularFileNameTpl() throws Exception { //该类中使用了SftpUtils的静态类的方法 JudgeForceFetchStep judgeForceFetchStep = new JudgeForceFetchStep(); Date date = DateUtils.yesterday(); //组装judgeForceFetchStep的入参数据 ChannelBillConfig channelBillConfig = newInstanceChannelBillConfig(); ChannelBillHistory channelBillHistory = newInstanceChannelBillHistory(); channelBillHistory.setFileSavePath(NameUtils.getNameWithDateTemplate(channelBillConfig.getSftpPathTpl(), date)); channelBillHistory.setBillName(NameUtils.getNameWithDateTemplate(channelBillConfig.getFilesNameTpl(), date)); //初始化入参捕获器 ArgumentCaptor<String> remotePath = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String[]> fileName = ArgumentCaptor.forClass(String[].class); ArgumentCaptor<String> ip = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> user = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> pwd = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> port = ArgumentCaptor.forClass(String.class); //对静态类进行mock PowerMockito.mockStatic(SftpUtils.class); //对静态类的void方法进行mock行为,不抛异常,不做任何逻辑执行 PowerMockito.doNothing().when(SftpUtils.class); //对要验证类中的静态方法void方法进行mock,并进行参数捕获 SftpUtils.deleteSftpFile(remotePath.capture(), fileName.capture(), ip.capture(), user.capture(), pwd.capture(), port.capture()); //执行要单测的类 boolean result=judgeForceFetchStep.perform(channelBillConfig, channelBillHistory, applicationContext, true, new ReportEvent(ReportEventType.BILL_HISTORY_CHECK), "/12345"); //验证返回结果 Assertions.assertTrue(!result); //验证sftpUtils的deleteSftpFile的调用次数 PowerMockito.verifyStatic(SftpUtils.class, Mockito.times(1)); SftpUtils.deleteSftpFile(Mockito.any(),Mockito.any(String[].class),Mockito.any(),Mockito.any(),Mockito.any(),Mockito.any()); //获取调用参数的入参 String remotePaths = remotePath.getValue(); String[] fileNames = fileName.getValue(); String ips = ip.getValue(); String users = user.getValue(); String pwds = pwd.getValue(); String ports = port.getValue(); String fileName1=fileNames[0]; String fileName2=fileNames[1]; String dateStr=DateUtils.getDateTimeStr(date, "yyyy-MM-dd"); Assertions.assertTrue(remotePaths.equals("/opt/funds/netpay/" + DateUtils.getDateTimeStr(date, "yyyyMMdd"))); Assertions.assertTrue(fileName1.equals(dateStr + "ebank.txt")); Assertions.assertTrue(fileName2.equals(dateStr + "ebank.txt.md5")); Assertions.assertTrue(ips.equals("sftp.meituan.sankuai.com")); Assertions.assertTrue(users.equals("supergw")); Assertions.assertTrue(pwds.equals("s123456")); Assertions.assertTrue(ports.equals("8080")); } }
浙公网安备 33010602011771号