0

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.

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.

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);
aufrisbee6
  • 1
  • 1
  • 1
  • So is what you're trying to do it set the label and then assert what the label is? Correct?? If so it looks close to me, You call the constructor in your test which has the label passed in as en empty string, the constructor sets the label. Your code gets the Cylinder class, calls getLabel with getLabel(label) [I believe only needs to be getLabel() not getLabel(label)] and then you assert what it is. In the assert though you have a space which you do not have when you start. You should assert that its an empty string. E.g. "" – Dale Oct 30 '15 at 03:22

1 Answers1

-1

In the above code you're effectively testing getters and setters which is not commonly accepted practice.

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.

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).

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.

Community
  • 1
  • 1
pdmoore
  • 66
  • 1
  • 1
  • 5