【发布时间】:2019-09-20 08:37:30
【问题描述】:
我已经完成了一个我一直在从事的大项目。我通常在没有 Junit 的情况下进行自己的测试,但我的要求是现在使用它。我要测试的所有方法都是无效的,不返回任何内容,而是根据某些因素打印信息。所以,我需要使用 Junit 的 assertEquals 方法测试这些。
例如:
public void addContact(String firstName,String lastName,Person p) {
String key = firstName.substring(0,1).toUpperCase() + firstName.substring(1).toLowerCase() + " ".concat(lastName.substring(0,1).toUpperCase() + lastName.substring(1).toLowerCase());
if(hasContact(key)){
System.out.println("\nCannot add user. User already exists. Try adding distinct name to diffentiate between users with the same name");
}
else{
this.contacts.put(key,p);
System.out.println("\nUser: " + key + " Successfully added");
}
}
这是我想从我的 AddressBook 类中测试的 void 方法之一,目前,我正在测试是否可以添加用户,因此它应该打印 \nUser: " + key + " Successfully added 它确实如此。
在我的 JUNIT 测试课程中,我正在尝试这样检查...
@Test
public void addContact(){
final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
Address a1 = new Address("grove","Waterford","Waterford","x9123r","0987654321");
Person p1 = new Person("Charlie","Ansell",a1);
System.setOut(new PrintStream(outContent));
ad1.addContact("Charlie","Ansell", p1);
assertEquals("User: Charlie Ansell Successfully added", outContent.toString());
}
Junit 的输出是:expected:<[User: Charlie Ansell Successfully added]> but was <[User: Charlie Ansell Successfully added]>
我的问题是,如果它们显示相同的输出,为什么会失败?
【问题讨论】:
-
仅仅因为它们看起来相同并不意味着它们实际上是相同的。有些字符看起来像其他字符。有些字符是不可见的。