Rendolph
Rendolph

Reputation: 431

Assert.assertEquals didn't compares String right way

I'm currently working on a udemy java programming class and struggling with an excercise.

Here the master soultion:

public class Bonbon {
    
    public static void bonbonRechner(){
        //Hier den Code der Main-Methode einfügen
        String Jan = "Jan";
        String Lisa = "Lisa";
        String Tom = "Tom";
        int Bonbons = 100;
        int JanBonbon = 6;
        int LisaBonbon = 11;
        int TomBonbon = 8;
        Bonbons = Bonbons - JanBonbon - LisaBonbon - TomBonbon;

        System.out.println(Jan + ": " + JanBonbon + ", " + Lisa + ": " + LisaBonbon + ", "
            + Tom + ": " + TomBonbon + ", Restlichen Bonbons: " + Bonbons);
        
    }
    public static void main(String[] args) {
        bonbonRechner();
    }
}

It should print Jan: 6, Lisa: 11, Tom: 8, Restlichen Bonbons:75

I configured the Evaluate.java like this:

import org.junit.Test;
import org.junit.Assert;
import com.udemy.ucp.*;
import com.udemy.ucp.IOHelper;

public class Evaluate {
    Bonbon bw = new Bonbon();
    IOHelper helper = new IOHelper();
    @Test
    public void testExercise() {
        helper.resetStdOut();
        bw.bonbonRechner();
        Assert.assertEquals("Not right", 
            "Jan: 6, Lisa: 11, Tom: 8, Restlichen Bonbons: 75", helper.getOutput());
    }
}

This is what my console says:

Console

Not right expected:<...stlichen Bonbons: 75[]> but was:<...stlichen Bonbons: 75[

Your output

Jan: 6, Lisa: 11, Tom: 8, Restlichen Bonbons: 75

]>

Any ideas how I can fix the problem with the missing ]> in my output?

Thanks in advance!

Upvotes: 0

Views: 39

Answers (1)

Simulant
Simulant

Reputation: 20142

System.out.println appends a new line \n after your text, which is part of the comparison as everything of StdOut is captured by your IOHelper, but your expected String does not contain a new line. You could use System.out.print instead to avoid this.

Upvotes: 3

Related Questions