繁体   English   中英

在JAVA中对字符串进行单元测试

[英]Unit Testing toStrings in JAVA

您如何在JAVA中测试toString? 我想这与常规测试的概念相同,但是我确实有很多问题。 我的Big JAVA教科书中找不到任何此类信息。 我已经阅读了至少5次,而没有读过的所有章节。 我只是没有看到它,而这在任何作业中都没有涉及。

对于一个例子,我不理解在第一个示例中在单元测试中的何处提供了代码,我的老师在assertTrue(text.contains(num))中放入了“ num”;

我发现她放了num,因为我们遵循的是“终身政策”的格式。 防爆。

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

我以为,由于这是一个示例,因此我将执行相同的操作,例如如下所示的面值。

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

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

因此,显然我的测试都没有通过。 这是我的代码。 对于WholeLifePolicy类,请紧随测试类。

为什么使用“ num”? 是否因为它是策略对象的显式参数? 还是仅仅是因为里面有一个数字”还是另一个原因?

显然我所有的代码都在// comments下面。 实验室源中提供了所有其他内容。

 /**
     * 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));



    }
}

您要测试的东西没有任何难以模拟的依赖关系。 测试这应该很容易。 使用contains使用多个测试实际上容易出错,因为它引入了复杂性。 保持简单明了。

这可能像

@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的静态方法(请参阅API) java.lang.Double的文档 )。

您可以在执行测试时将double类型的value解析为字符串。

显然,您必须完成缺失值的测试。 Num已被测试包含在toString方法中。

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

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

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

此方法也应该起作用:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM