Below is my Cylinder class, and then the JUnit test that I am trying to use to test the getLabel method I wrote. I am having trouble understanding how to properly form a test method when I am testing for a string that the user will input.
下面是我的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;
}
Below is my JUnit test class, which I am using to create a test for each method in my Cylinder class.
下面是我的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);
1 个解决方案
#1
-1
In the above code you're effectively testing getters and setters which is not commonly accepted practice.
在上面的代码中,您有效地测试了不常见的实践中的getter和setter。
When you pass in a label to the constructor you expect that to be the Cylinder's label. No transformation takes place. You're simply testing that a variable can be set to a value, which isn't likely to break. We want to focus our tests on behavior.
当您将标签传递给构造函数时,您希望它是Cylinder的标签。没有发生转变。您只是测试变量可以设置为一个不太可能破坏的值。我们希望将测试重点放在行为上。
However, in your labelTest above you pass in an empty string to the constructor but expect a space character in the assertEquals. If this is the desired behavior it does warrant a test. You should also name the test to indicate why this behaves that way (test_GetLabel_ExpandsEmptyStringToSpace or something similar).
但是,在上面的labelTest中,您将空字符串传递给构造函数,但期望assertEquals中有空格字符。如果这是期望的行为,则确实需要进行测试。您还应该命名测试以指示其行为方式(test_GetLabel_ExpandsEmptyStringToSpace或类似的东西)。
In your code, I would focus more on how the parameters interact. For instance, given a radius and a height you could write a volume test that shows how the two parameters relate to each other. For instance
在您的代码中,我将更多地关注参数如何交互。例如,给定半径和高度,您可以编写一个体积测试,显示两个参数如何相互关联。例如
@Test public void test_VolumeCalculation() {
Cylinder c1 = new Cylinder("", 2.0, 4.5);
String actual = c1.getVolume();
Assert.assertEquals(56.55, actual, 0.001);
}
I have encountered some code where the parameters get jumbled so you could consider writing tests that show the radius and height values are assigned to the correct field. Since they are both doubles it is possible to pass in the values in the wrong order but the test can only prove that you assigned the values correctly and won't prevent a caller from mixing them up.
我遇到了一些代码混乱的代码,因此您可以考虑编写测试,显示半径和高度值分配给正确的字段。由于它们都是双精度数,因此可能以错误的顺序传递值,但测试只能证明您正确分配了值并且不会阻止调用者将它们混合起来。
Your code also has four parameters in the constructor but only three appear in the constructor call in the test. You may want to clean up your question to pinpoint what you're asking.
您的代码在构造函数中也有四个参数,但在测试中只有三个参数出现在构造函数调用中。您可能想要清理您的问题以查明您的要求。
#1
-1
In the above code you're effectively testing getters and setters which is not commonly accepted practice.
在上面的代码中,您有效地测试了不常见的实践中的getter和setter。
When you pass in a label to the constructor you expect that to be the Cylinder's label. No transformation takes place. You're simply testing that a variable can be set to a value, which isn't likely to break. We want to focus our tests on behavior.
当您将标签传递给构造函数时,您希望它是Cylinder的标签。没有发生转变。您只是测试变量可以设置为一个不太可能破坏的值。我们希望将测试重点放在行为上。
However, in your labelTest above you pass in an empty string to the constructor but expect a space character in the assertEquals. If this is the desired behavior it does warrant a test. You should also name the test to indicate why this behaves that way (test_GetLabel_ExpandsEmptyStringToSpace or something similar).
但是,在上面的labelTest中,您将空字符串传递给构造函数,但期望assertEquals中有空格字符。如果这是期望的行为,则确实需要进行测试。您还应该命名测试以指示其行为方式(test_GetLabel_ExpandsEmptyStringToSpace或类似的东西)。
In your code, I would focus more on how the parameters interact. For instance, given a radius and a height you could write a volume test that shows how the two parameters relate to each other. For instance
在您的代码中,我将更多地关注参数如何交互。例如,给定半径和高度,您可以编写一个体积测试,显示两个参数如何相互关联。例如
@Test public void test_VolumeCalculation() {
Cylinder c1 = new Cylinder("", 2.0, 4.5);
String actual = c1.getVolume();
Assert.assertEquals(56.55, actual, 0.001);
}
I have encountered some code where the parameters get jumbled so you could consider writing tests that show the radius and height values are assigned to the correct field. Since they are both doubles it is possible to pass in the values in the wrong order but the test can only prove that you assigned the values correctly and won't prevent a caller from mixing them up.
我遇到了一些代码混乱的代码,因此您可以考虑编写测试,显示半径和高度值分配给正确的字段。由于它们都是双精度数,因此可能以错误的顺序传递值,但测试只能证明您正确分配了值并且不会阻止调用者将它们混合起来。
Your code also has four parameters in the constructor but only three appear in the constructor call in the test. You may want to clean up your question to pinpoint what you're asking.
您的代码在构造函数中也有四个参数,但在测试中只有三个参数出现在构造函数调用中。您可能想要清理您的问题以查明您的要求。