2015-10-23 29 views
1

如何在JAVA中测试toString?我想这和普通测试是一样的概念,但我真的有很多问题。在我的Big JAVA教科书中找不到这些信息。我已阅读了至少5次所有章节。我只是没有看到它,而这在任何作业中都没有涉及。JAVA中的单元测试toStrings

对于其中一个,我不明白在第一个例子中代码的单元测试中,我的老师在assertTrue(text.contains(num))中放入了“num”;

我觉得她把num放进去,因为我们遵循WholeLife策略的格式。防爆。

WholeLifePolicy policy = new WholeLifePolicy(num, value, years, name); 

我以为这是一个例子,我会做同样的事情,例如面值如下所示。

assertTrue(text.contains("Face Value:")); 
     assertTrue(text.contains(value)); 

这不会编译,因为它表示它不兼容,因为数字是双精度型。所以我尝试了两次。唯一编译的是“num”

所以很明显,我的测试都没有通过。这是我的代码。对于WholeLifePolicy类,请遵循测试类。

为什么使用“num”?它是否因为它是策略对象的显式参数?或者是仅仅因为一个数字是在那里”或其他原因

显然我所有的代码是低于//评论其他的一切在实验室源还

/** 
    * Return a string representation of the WholeLifePolicy. 
    * 
    * @return String output string. 
    * 
    * <pre> 
    * Produce output in the following format: 
    * 
    * Policy Information: 
    * Policy #: WLP1000000 
    * Policy Years: 20 
    * Face Value: 50000.0 
    * Beneficiary: John Doe 
    * 
    * </pre> 
    */ 
    public String toString() 
    { 
     String output = "Policy Information:\n"; 
     output = output + "Policy #: " + this.getPolicyNum() + "\n"; 

     // your code here, finish the output string 
     output = output + "Face Value" + this.getFaceValue() + "\n"; 
     output = output + "Policy Years:" + this.getPolicyYrs() + "\n"; 
     output = output + "Beneficiary" + this.getBeneficiary() + "\n"; 
     return output; 

    } 

} 

测试类:。

/** 
    * Test the toString method. 
    */ 
    @Test 
    public void testToString() 
    { 
     String num = "WLP1234567"; 
     double value = 50000.0; 
     int years = 20; 
     String name = "Katie Perry"; 
     WholeLifePolicy policy = new WholeLifePolicy(num, value, years, name); 

     String text = policy.toString(); 
     assertTrue(text.contains("Policy Information:")); 

     assertTrue(text.contains("Policy #:")); 
     assertTrue(text.contains(num)); 

     // your code here, finish the testing of the toString() method 
     // checking for both the headings and face value, policy years 
     assertTrue(text.contains("Face Value:")); 
     assertTrue(text.contains(num)); 


     assertTrue(text.contains("Policy Years:")); 
     assertTrue(text.contains(num)); 

     assertTrue(text.contains("Beneficiary:")); 
     assertTrue(text.contains(num)); 



    } 
} 
+0

这是一个很好的问题。这是因为这本Big Java书籍非常简短地介绍了2页的单元测试,而且我完全搞不清楚这些测试是如何测试事情的,以及它们如何甚至是连接到普通课程。我真的需要为初学者编写的初学者信息,它将单元测试分解成不同的部分,并解释每个部分的作用和原因。我在许多,许多小时之后仍然在为这个toString类而努力。 – jforrest1980

回答

0

您可以在执行测试时显然解析double类型value为一个字符串。

0

你必须完成你的检查失踪VA梅毒。 Num已经被测试包含在toString方法中。

现在您应该测试toString方法的输出中包含的其余属性。

例如,你可以使用一个空字符串强制转换为字符串:

assertTrue(text.contains("Face Value:")); 
// this converts 'value' to string 
assertTrue(text.contains("" + value)); 

这种方法也应该工作:

assertTrue(text.contains(Double.toString(value))); 
1

你测试没有任何依赖的东西这很难嘲笑。测试这应该很容易。使用包含的多个测试实际上是容易出错的,因为它引入了复杂性。保持简单直接。

这可能是那么容易,因为

@Test 
public void toString() { 
    WholeLifePolicy policy = new WholeLifePolicy("WLP1234567", 
    50000.0, 20, "Katie Perry"); 
    String expected = "Policy Information:\nPolicy #: WLP1000000\n" 
    + "Policy Years: 20\nFace Value: 50000.0\nBeneficiary: John Doe\n"; 
    assertEquals(expected, policy.toString()); 
} 

此检查,当你希望他们输入的值表示。如果断言失败,那么错误消息会告诉你哪里存在不匹配。对期望的字符串进行硬编码很好,您不需要使用与设置测试相同的值。如果你这样做,你会复制你在测试方法中使用的相同代码,这将是很多工作(不只是前期,但每次代码改变)。

这里最大的现实世界复杂是使用换行符。通常,开发人员可以在公司强制的Windows上本地运行测试,并在具有不同行尾的某个其他平台上由CI执行测试。如果我想不管这个工作的行结束我会改变了toString到应该使用System.getProperty(“line.separator”)和我的测试更改为类似

static String lines(List<String> strings) { 
    String delimiter = System.getProperty("line.separator"); 
    StringBuilder builder = new StringBuilder(""); 
    for (String s : strings) { 
     builder.append(s); 
     builder.append(delimiter); 
    } 
    return builder.toString(); 
} 

@Test 
public void testToString() { 
    WholeLifePolicy policy = new WholeLifePolicy("WLP1234567", 
    50000.0, 20, "Katie Perry"); 
    List<String> expectedLines = new ArrayList<String>(); 
    expectedLines.add("Policy Information:"); 
    expectedLines.add("Policy #: WLP1000000"); 
    expectedLines.add("Policy Years: 20"); 
    expectedLines.add("Beneficiary: John Doe"); 
    assertEquals(lines(expectedLines), policy.toString()); 
} 

其实因为:a)我很懒惰,b)toStrings中的换行符会造成无序的日志文件,我避免在toString方法中使用行分隔符。

这可能是一个练习,旨在教授如何调用方法,以及如何查找API文档(这两者都是重要的生存技能)。 String#contains方法将CharSequence作为参数(String实现),因此您需要将您的变量转换为String以使用它。如果你有一个引用类型,你可以在它上面调用toString,对于基本类型,你可以查找包装类型,并将原语转换为包装类型,或者找到一个静态方法,它接受原语并将其转换为String(参见the API documentation for java.lang.Double) 。

+0

我很感谢你的深入解释,这真的有所帮助。不幸的是,我不允许改变测试用例。我只允许在下面添加我的代码,它说//在此处添加您的代码。 – jforrest1980

+0

@ jforrest1980:好的,检查包含方法,它需要一个字符串。你可以在参数上调用toString,这样你传递给方法的就是一个字符串。 –