/** * CS149 - Programming Fundamentals * Department of Computer Science * James Madison University * @version Fall 2019 */
The best way to ensure correctness is to test thoroughly while software is being developed. JUnit is a framework that automates testing by providing a standard format for tests and an easy way to execute them (see JUnit FAQ).
First download the following two files and store them in a location where you will be able to find them later:
Now, start jGRASP and open the JUnit configuration dialog: Tools -> JUnit -> Configure. For "JUnit Home" Select the folder where you stored the jar files above and click "OK".
If all went well the "Create JUnit test file" button
JUnit Test button should now appear whenever you
open a Java file in jGRASP. Clicking that button will auto-generate a
starter test class with the appropriate imports.
Here are some sample JUnit test methods for HW 4.3:
/** Unit tests for fromCelsiusToFahrenheit **/
@Test
public void fromCelsiusToFahrenheitTest() {
double converting;
double expected;
double actual;
converting = 100.0;
expected = 212.0;
actual = Convert.fromCelsiusToFahrenheit(converting);
Assert.assertEquals(expected, actual, .0001);
converting = 0.0;
expected = 32.0;
actual = Convert.fromCelsiusToFahrenheit(converting);
Assert.assertEquals(expected, actual, .0001);
converting = 1.0;
expected = 33.8;
actual = Convert.fromCelsiusToFahrenheit(converting);
Assert.assertEquals(expected, actual, .0001);
}
/** Unit tests for fromFahrenheitToCelsius **/
@Test
public void fromFahrenheitToCelsiusTest() {
double converting;
double expected;
double actual;
converting = 212.0;
expected = 100.0;
actual = Convert.fromFahrenheitToCelsius(converting);
Assert.assertEquals(expected, actual, .0001);
converting = 32.0;
expected = 0.0;
actual = Convert.fromFahrenheitToCelsius(converting);
Assert.assertEquals(expected, actual, .0001);
converting = 33.8;
expected = 1.0;
actual = Convert.fromFahrenheitToCelsius(converting);
Assert.assertEquals(expected, actual, .0001);
}
If you've already completed HW 4.3, download your submission from
Autolab and use JUnit to test it locally using these tests. First you
will need to open your Convert.java
file in jGRASP, then you can
click on the "Create JUnit test file" button to create a starter testing class. Then you
can copy these testing methods into that class. Try writing an
additional test method for
fromKilometersToMiles
.
If you haven't yet completed HW 4.3, go ahead and write the first few methods and test them as described above.