mardi 23 juin 2020

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

I have a method that returns a String and I would like to test if it works correctly. For that I have a test.txt-file that I read and compare to the return value of my method. If I print both Strings out they are exactly the same! Somehow assertEquals still fails.. What am I doing wrong here?

Method to test:

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:

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

Output: 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

Output: statement

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

readFile:

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

Aucun commentaire:

Enregistrer un commentaire