如何在 java 中使用 Junit 和 mockito 对读取路径和文件的方法进行单元测试

问题描述 投票:0回答:0

我是 Java 中 Junit 和 Mockito 测试的新手,有人可以给我一个示例或想法,说明我如何测试一个基本上读取路由和文件并接收日期作为参数的方法:

public String getFileAE(String fecha) {
    String fileName = null;
    String filePath = null;
    File file = null;
    
    try {
        logger.info("Leyendo archivo ejecutivos facultados");
        fileName = ConstantsFiles.FILE_NAME_AE + fecha + ConstantsFiles.FILE_EXTENSION;
        filePath = ConstantsFiles.FILE_PATH_AE + fileName;
        logger.info(filePath);
        
        file = new File(filePath);
        if (!file.exists()) {
            logger.error("Error, el archivo no existe: " + filePath);
            return "";
        }
        return filePath;
    } catch (Exception ex) {
        logger.error("Error, el archivo no se pudo leer: " + ex.getMessage(), ex);
    }
    return filePath;    
}
java junit mockito junit4
© www.soinside.com 2019 - 2024. All rights reserved.