gpt4 book ai didi

java - 我应该如何为这个函数编写 JUnit

转载 作者:行者123 更新时间:2023-11-30 05:24:33 24 4
gpt4 key购买 nike

public static Map<String,SrnPastPricing> parseFile(String fileLoc) throws IOException {
String fileAsString = FileUtils.readFileToString(new File(FilenameUtils.normalize(fileLoc)));
fileAsString = fileAsString.replace("\r", "");
String[] lines = fileAsString.split("\\n");
String headingsRow = lines[0];
String[] headings = headingsRow.split(",");
System.out.println("Reading first line as the heading row...");

Map<String,SrnPastPricing> pastPricingMap = new HashMap<String,SrnPastPricing>();
for (int x = 1; x < lines.length; x++) {
SrnPastPricing pastPricing = parseLine(lines[x], headings);
pastPricingMap.put(pastPricing.ClaimId, pastPricing);
}
return pastPricingMap;
}

我尝试模拟它并返回 map ,但它要求文件的确切位置,并且不适用于随机字符串参数并抛出空指针异常。

SrnPastPricing mockSrnPastPricing = Mockito.spy(srnPastPricing);
Mockito.doReturn(mockMap).when(mockSrnPastPricing).parseFile("file\\loc\\file.txt");

请告诉我模拟它的编写方式。

最佳答案

如果它是静态文件,我通常将示例文件放入 src/resources 文件夹中,并从类路径加载该文件。

如果您在实例内部运行,this.getClass().getResource("/log4j.properties").getFile()

如果您作为静态方法运行,CurrentJavaClassName.class.getResource("/log4j.properties").getFile()

关于java - 我应该如何为这个函数编写 JUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58921011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com