简体   繁体   English

Assert.assertEquals 没有正确比较 String

[英]Assert.assertEquals didn't compares String right way

I'm currently working on a udemy java programming class and struggling with an excercise.我目前正在使用 udemy java 编程 class 并在练习中苦苦挣扎。

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它应该打印Jan: 6, Lisa: 11, Tom: 8, Restlichen Bonbons:75

I configured the Evaluate.java like this:我像这样配置了 Evaluate.java:

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?有什么想法可以解决我的 output 中缺少 ]> 的问题吗?

Thanks in advance!提前致谢!

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. System.out.println在您的文本之后追加一个新行\n ,这是比较的一部分,因为您的 IOHelper 捕获了 StdOut 的所有内容,但您预期的 String 不包含新行。 You could use System.out.print instead to avoid this.您可以改用System.out.print来避免这种情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Assert.assertEquals的奇怪行为 - Strange behaviour of Assert.assertEquals Assert.assertEquals在两个列表上 - Assert.assertEquals on two lists 如果在Assert.assertEquals硒测试NG上有其他条件 - if else condition on Assert.assertEquals selenium testNG assertEquals()和Assert.assertEquals()之间的JUnit测试差异 - JUnit test difference between assertEquals() and Assert.assertEquals() java-有时返回字符串且有时返回null时如何使用Assert.assertEquals - java - How to use Assert.assertEquals when sometimes a String is returned and sometimes null is returned 如何使用 Assert.assertEquals() 测试单链表 - How to test Single Linked List using Assert.assertEquals() Selenium可以对测试(Assert.assertEquals())失败进行截图吗? - Can Selenium take a screenshot on test(Assert.assertEquals()) failure? Selenium Web驱动程序-Java-TestNG-Assert.assertEquals如何将实际结果与预期结果之间的范围进行比较 - Selenium Web Driver - Java - TestNG - Assert.assertEquals how to compare actual result with the range between expected result selenium TestNG 中的断言(assert 类型的 assertEquals(String, String) 方法未定义) - Asserts in selenium TestNG (The method assertEquals(String, String) is undefined for the type Assert) 声明类型中的方法assertEquals(Object,object)不适用于参数(String,Void) - Method assertEquals(Object, object) in the type Assert is not applicable for the arguments (String, Void)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM