• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

无信不立

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

【单元测试】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"));
    }
}
View Code

 

posted on 2020-05-14 20:59  无信不立  阅读(212)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3