测试字符串是否与 Junit returns false 相等即使相等

Test if strings are equal with Junit returns false even if its egual

我有一个 return 是 String 的方法,我想测试它是否正常工作。为此,我有一个 test.txt 文件,我读取该文件并将其与我的方法的 return 值进行比较。如果我将两个 Strings 都打印出来,它们是完全一样的!不知何故 assertEquals 仍然失败..我在这里做错了什么?

测试方法:

public String statement() {
    String result = "Rental Record for " + getName() + "\n";

    int frequentRenterPoints = 0;
    for (Rental each : this.rentals) {
        frequentRenterPoints += each.getFrequentRenterPoints();

        // show figures for this rental
        result += "\t" + each.getMovie().getTitle() + "\t"
                + " (" + each.getMovie().getQuality() + ")"
                + ": "
                + String.valueOf(each.getCharge()) + "\n";
    }

    // add footer lines
    result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
    result += "You earned " + String.valueOf(frequentRenterPoints)
            + " frequent renter points";
    return result;
} 

测试:

    @Test
public void statementReturnsCorrectlyFormattedString() throws IOException {
    // given
    customer = new Customer("ElonMusk");
    
    Movie movieOne = new Movie("IronMan1", PriceCodes.REGULAR, Quality.HD);
    Movie movieTwo = new Movie("AvengersEndGame", PriceCodes.NEW_RELEASE, Quality.FOUR_K);
    
    Rental rentalOne = new Rental();
    rentalOne.setMovie(movieOne);
    rentalOne.setDaysRented(5); 
    
    Rental rentalTwo = new Rental();
    rentalTwo.setMovie(movieTwo);   
    rentalTwo.setDaysRented(1);
    
    List<Rental> rentalList = new LinkedList<Rental>();
    rentalList.add(rentalOne);
    rentalList.add(rentalTwo);
    
    customer.setRentals(rentalList);
    String expectedString = "";
    try {
        expectedString = readFile("test.txt");
        System.out.println("expected: " + "\n" +expectedString);
    } catch (IOException e) {
        throw new IOException("Error reading statementTestFile!", e);
    }
    
    // when
    String statement = customer.statement();
    
    // then
    System.out.println("statement: " + "\n" + statement);
    System.out.println(expectedString.equals(statement));
    assertEquals(expectedString, statement); 
}

输出:expectedString

expected: Rental Record for ElonMusk IronMan1 (HD): 6.5 AvengersEndGame (FOUR_K): 5.0 Amount owed is 11.5 You earned 2 frequent renter points

输出:语句

statement: Rental Record for ElonMusk IronMan1 (HD): 6.5 AvengersEndGame (FOUR_K): 5.0 Amount owed is 11.5 You earned 2 frequent renter points

读取文件:

private String readFile(String file) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader (file));
    String         line = null;
    StringBuilder  stringBuilder = new StringBuilder();
    String         ls = "\n";

    try {
        while((line = reader.readLine()) != null) {
            stringBuilder.append(line);
            stringBuilder.append(ls);
        }

        return stringBuilder.toString();
    } finally {
        reader.close();
    } 
}

问题出在您从文件读取时添加的尾随换行符中。您可以在 trim 中输入字符串,但是如果您要读取的文件末尾有一些空行怎么办?

所以你可以像这样引入一个 'first line' 布尔值:

boolean isFirstLine = true;
while((line = reader.readLine()) != null) {
    if (!isFirstLine) {
        stringBuilder.append(ls);
    }
    stringBuilder.append(line);
    isFirstLine = false;
}

或者让循环保持原样,在它运行后从构建器中删除最后一个字符:

if (stringBuilder.length() > 0) {
    stringBuilder.deleteCharAt(stringBuilder.length() - 1); // or stringBuilder.lastIndexOf("\n");
}

或者做一个子字符串。

或者将这些行读入 List 集合,例如 ArrayList,然后再 String.join("\n", linesCollection);