【发布时间】:2018-03-28 11:29:49
【问题描述】:
public class Room extends java.lang.Object implements java.io.Serializable {
public String description;
public Map<String, Room> map;
public List<Thing> things;
//Room
//Parameters:
//desc - Description for the room Note: any newlines in desc will be
replaced with *
public Room(java.lang.String desc){
this.description = desc;
}
//getDescription
//A description of the room
//Returns:
//Description
public java.lang.String getDescription(){
return description;
}
//replaceLine
//any new line in s will be replaced with *
public String replaceLine(String s){
return s.replace("\n", "*");
}
//setDescription
//Change Room description Note: any newlines in s will be
//replaced with *
//Parameters:
//s - new Description
public void setDescription(java.lang.String s){
this.description = replaceLine(s);
}
}
我正在尝试为最后一个方法编写一个 Junit 测试:
@Test
public void setDescription(java.lang.String s) {
String input = "abc\n";
String expected = "abc";
setDescription(input);
assertEquals(expected, input);
}
我知道这是不正确的,但我不知道如何解决它。我对 Java 和整个编码都很陌生。有人可以帮我吗?
【问题讨论】:
-
你是如何在测试中构造
Room对象的? -
你为什么期望
"abc"会是"abc*"? -
UnitTests 验证单元的公共可观察行为,即:返回值和与依赖项的通信。 setter 方法仅更改我们未验证的对象的内部状态。
-
@TimothyTruckle 我会说这是公开可观察的,因为我们有一个吸气剂
-
@user7 getter 无关紧要 - 它不应该被测试。在这里,您可能出于不同的原因想要对其进行测试 - 它不仅仅是 setter,它内部还有一个逻辑。 stackoverflow.com/questions/6197370/…
标签: java