Unit Testing

The goal of this lab is to gain experience writing JUnit tests.

Part 1 - Coverage

JMU has the following severe weather cancellation policy*.

JMU SEVERE WEATHER POLICY

If any of the following conditions are met, campus will be closed:

  • More than 6.0 inches of rain are predicted within the next 24 hours.
  • Wind speeds greater than 70.0 mph are predicted within the next 24 hours.
  • It is predicted that there will be more than 4.0 inches of rain and wind speeds will be above 45.0 mph.

If none of the above conditions are met, the University may still issue a warning and encourage individuals to avoid non-essential outdoor activities. A warning will be issued if any of the following conditions are met:

  • Predicted wind speeds are above 45.0 mph.
  • Predicted precipitation is above 4.0 inches.

The following Java class has been developed as part of a JMU Decision Support system:

The weatherAdvice method provides an implementation of the cancellation policy above. Your job is to develop a set of JUnit tests to confirm that this method is implemented correctly. Your testing must also confirm that the correct exception is thrown when the method receives invalid input. Refer to the Javadoc for a detailed specification.

You may use the following class as a starting point for your testing:

Once you are confident that your unit tests are sufficient, Upload both WeatherUtils.java and WeatherUtilsTest.java to Web-CAT. The submission tests for this lab are configured to check the code coverage provided by your tests. Your goal is to achieve 100% method, statement, and branch coverage.

Part 2 - Catching Errors

100% coverage doesn't necessarily mean that your tests are good. High quality tests should be able to uncover errors in the code that is being tested. The next step is to run your tests against an implementation that is known to contain errors. If your tests are effective, they should indicate that there is a problem with this code. For this part of the lab you will run your tests against a pre-compiled .class file that I have intentionally coded to contain at least one error.

  1. Create a new Java Project.
  2. Download the file WeatherUtils.class.
  3. Go to "Project / Properties" and click on "Java Build Path".
  4. Click on "Libraries" -> "Add Class Folder" -> "New Folder". Give your folder a name such as "classes".
  5. Press "Okay". You will see a new section in the Package Explorer window called "Referenced Libraries" and you should see your new folder beneath it.
  6. In the Eclipse window, drag and drop your .class files into the folder you just created.
  7. Copy your completed WeatherUtilsTest.java file into the new project, and make sure that JUnit 4 is on the project build path.
  8. Execute the tests to confirm that at least one test fails.

* Not really.