gpt4 book ai didi

java - JUnit AssertionFailedError : Text in the file differs in java

转载 作者:行者123 更新时间:2023-12-03 08:51:03 25 4
gpt4 key购买 nike

我正在使用JUnit Test进行测试,但遇到了AssertionFailedError问题。
我正在使用命令行参数将测试用例传递给主类。

下面是我的 Main.java 代码

public class Main {

public static void main(String[] args) throws IOException {


//Storing all the commands, words, files
ArrayList<String> commands = new ArrayList<>();
ArrayList<String> words = new ArrayList<>();
ArrayList<String> files = new ArrayList<>();

for(String arg: args){
if(arg.contains("."))
files.add(arg);
else if(arg.contains("-") && !arg.contains("--"))
commands.add(arg);
else{
if(!arg.contains("--"))
words.add(arg);
}
}

for(String file : files ){

File originalFile = new File(file);

//CHECK IF textFile exists
if(originalFile.exists()){
if(words.size() == 2){
String from = words.get(0), to=words.get(1);
BufferedWriter bw;
//If file exists then check command
for(String command : commands){
if(command.trim().contains("-f")){
File temp = new File("Temp.txt");
temp.createNewFile();
//If Temp.exists
if(temp.exists()){
bw = new BufferedWriter(new FileWriter(temp));

//Fetch all the lines from Orginal File
List<String> lines = Files.readAllLines(Paths.get(originalFile.getName()));

//Add to treemap
TreeMap<Integer,String> tm = new TreeMap<>();
for(int i=0;i<lines.size();i++){
tm.put(i,lines.get(i));
}

//To check first occurence of word in hashmap
for(int i=0;i<tm.size();i++){
String lastLine = tm.get(i);
tm.remove(i);
tm.put(i,lastLine.replaceFirst(from,to));
if(!lastLine.equals(tm.get(i)))
break;
}

//Write treemap to the text file
for(String line: tm.values())
bw.write(line.trim() + "\n");
System.out.println("First Occurence " + originalFile.getName()+ " changed");
bw.close();
originalFile.delete();
temp.renameTo(originalFile);
}else
System.out.println("Error in creating Temp.txt file");
}
}
}

一切正常,文件已创建。我认为代码中没有错误。下面是 MainTest.java
public class MainTest {

// Some utilities

private File createInputFile1() throws Exception {
File file1 = new File("Test.txt");
try (FileWriter fileWriter = new FileWriter(file1)) {
fileWriter.write("Dog is an animal");
}
return file1;
}

private String getFileContent(String filename) {
String content = null;
try {
content = new String(Files.readAllBytes(Paths.get(filename)));
} catch (IOException e) {
e.printStackTrace();
}
return content;
}

// Actual test cases
@Test
public void mainTest2() throws Exception {
File inputFile1 = createInputFile1();

String args[] = {"-f", "Dog", "Cat", "--", "Test.txt"};
Main.main(args);

String expected1 = "Cat is an animal".trim();

String actual1 = getFileContent("Test.txt");

assertEquals("The files differ!", expected1, actual1);
assertTrue(Files.exists(Paths.get(inputFile1.getPath() + ".bck")));
}

}

一切正常,创建文件Test.txt并包含文本。
但是我遇到了这个AssertionFailedError错误: 文件不同!预期:猫是动物[],但是:猫是动物[]

为什么[]和[]不同?

最佳答案

试试这个,将会有所帮助:

String expected1 = "Cat is an animal".trim();

String actual1 = getFileContent("Test.txt").trim();

assertEquals("The files differ!", expected1, actual1);

由于 trim所做的是

 * @return  A string whose value is this string, with any leading and trailing white
* space removed, or this string if it has no leading or
* trailing white space.


因此,您在解决方案中会看到 [ ][]的空间差异,并且两者都使用trim进行了解析。

关于java - JUnit AssertionFailedError : Text in the file differs in java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40661681/

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