如何在 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.