【发布时间】:2015-10-30 03:10:31
【问题描述】:
下面是我的 Cylinder 类,然后是我试图用来测试我编写的 getLabel 方法的 JUnit 测试。在测试用户将输入的字符串时,我无法理解如何正确形成测试方法。
public class Cylinder {
private String label = "";
private double radius;
private double height;
private static int count = 0;
/**
* Creates a new Cylinder object with a given label, radius, and height.
*
* @param label2 The name of the cylinder returned as a String.
* @param radius2 The radius of the cylinder as a double.
* @param height2 The height of the cylinder as a double.
*/
public Cylinder(String label2, double radius2, double height2, int count2) {
setLabel(label2);
setRadius(radius2);
setHeight(height2);
setCount(count2);
}
/**
* This method is respondible for getting the label from the user
* and returns a string representation of the label.
*
* @return String representation of the label of the Cylinder.
*/
public String getLabel() {
return label;
}
下面是我的 JUnit 测试类,我用它来为我的 Cylinder 类中的每个方法创建一个测试。
public class CylinderTest {
private String label = "";
private double radius;
private double height;
/*
*
*/
@Test public void labelTest() {
Cylinder c1 = new Cylinder("", radius, height);
String result = c1.getLabel(label);
Assert.assertEquals(" ", label);
【问题讨论】:
-
那么你想要做的是设置标签然后断言标签是什么?正确的??如果是这样,它看起来很接近我,您在测试中调用构造函数,该构造函数将标签作为空字符串传入,构造函数设置标签。您的代码获取 Cylinder 类,使用 getLabel(label) [我相信只需 getLabel() 而不是 getLabel(label)] 调用 getLabel,然后断言它是什么。在断言中,尽管您有一个开始时没有的空间。您应该断言它是一个空字符串。例如。 ""
标签: java unit-testing testing methods junit