【问题标题】:Test if strings are equal with Junit returns false even if its egual使用 Junit 测试字符串是否相等,即使它相等也返回 false
【发布时间】:2020-06-23 11:37:12
【问题描述】:

我有一个返回String 的方法,我想测试它是否正常工作。为此,我有一个test.txt-文件,我读取并与我的方法的返回值进行比较。如果我将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); 
}

输出:预期字符串

预计: ElonMusk 的租赁记录 钢铁侠1(高清):6.5 AvengersEndGame (FOUR_K):5.0 欠款为 11.5 您获得了 2 个常客积分

输出:语句

声明: ElonMusk 的租赁记录 钢铁侠1(高清):6.5 AvengersEndGame (FOUR_K):5.0 欠款为 11.5 您获得了 2 个常客积分

读取文件:

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();
    } 
}

【问题讨论】:

  • 你做了修剪吗,你检查了不可打印的字符吗?
  • 还没有,我现在试试
  • 好的,所以assertEquals(expectedString.trim(), statement.trim()); 实际上正在工作。但为什么呢?
  • @Stultuske 在test.txt 我输入的正是我所期望的。我的readFile 添加了不可打印的字符有什么问题吗?我在问题中添加了readFile
  • 你自己添加。阅读每一行后添加\n,它也会被添加到最后一行之后。

标签: java testing junit integration-testing


【解决方案1】:

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

因此,您可以像这样引入“第一行”布尔值:

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多