[英]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.