Java Reference
In-Depth Information
fail("Not yet implemented");
}
@Test
public void testConvertCmtoFeet() {
fail("Not yet implemented");
}
@Test
public void testConvertFeettoCm() {
fail("Not yet implemented");
}
@Test
public void testConvertCmtoInches() {
fail("Not yet implemented");
}
@Test
public void testConvertInchestoCm() {
fail("Not yet implemented");
}
}
7. Add some class variables that you can use in the tests: double feet , meters , inches , and cm .
8. Fill in the body of your first test method, the testConvertFeettoMeters() method, like so:
@Test
public void testConvertFeettoMeters() {
feet = 1;
meters = DistanceConverter.convertFeettoMeters(feet);
assertEquals(meters, 0.3048,0.001);
}
To convert feet to meters, you should give a value for feet and the method will calculate the equiva-
lent in meters. Therefore, in your test case, assign a value to the variable feet . Use the method to
assign a value to the variable meters . Then calculate the equivalent in meters. Use the assertE-
quals() method to make sure the method calculation is equal to your own calculation. Because of
the rounding issues with doubles, the assertEquals() method for doubles has an extra parameter,
called epsilon , where you can indicate how close the two doubles should be to be considered equal.
In this example, using 0.001 for epsilon , you are saying that if the calculated value for meters is
within 0.001 of 0.3048, you should consider them equal.
9. Try to fill in the rest of the test methods in the same way. Give a value for the first variable. Use
the method to calculate the second variable. Then compare the expected true conversion to the
method's return value.
10. Your test case should resemble this after you've filled in all the test methods:
import static org.junit.Assert.*;
import org.junit.Test;
Search WWH ::




Custom Search