Unit Testing and Code Coverage

The goal of this lab is to gain experience using code coverage tools and writing high-quality 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.

Creating a JUnit Test Class

Once you have set up an Eclipse project containing WeatherUtils.java, create a stub JUnit test class:

  1. Right click the WeatherUtils.java in the Package Explorer and select New -> JUnit Test Case. Make sure "New JUnit 4 test" is selected. Accept the default options and click "Finish". This should create a new file named WeatherUtilsTest.java.
  2. Here are two testing methods to get you started:
        @Test
        public void testWindspeedOnlyAboveDangerThreshold()
        {
            assertEquals("CANCEL", WeatherUtils.weatherAdvice(70.1, 0.0));
        }
    
        @Test
        public void dummyTestToCallConstructorForFullCoverage()
        {
            // Web-CAT will report < 100% coverage if the constructor for
            // WeatherUtils is never called.
            new WeatherUtils();
        } 
    				     
  3. Once you have set up your JUnit test class you can run the tests by selecting Run -> RunAs -> JUnit Test from the Eclipse menu.

Setting Up EclEmma

EclEmma is a free Java code coverage tool for Eclipse. Follow the steps below to integrate EclEmma into your Eclipse environment. The Lab machines might already have this feature. In that case skip Step 1 and directly continue to Step 2.
  1. Go to Help -> Eclipse Marketplace, find EclEmma, and click Install. This plugin will allow you to run 'Code Coverage' tests (like Web-CAT does) against your program to see the coverage of your test files against your code.
  2. You can now run coverage tests against your program by right-clicking your project and selecting 'Coverage As' -> JUnit Test. This should color-code your Java files to indicate which lines are being exercised by your tests. You should also see a new tab labeled "Coverage" that will display coverage percentages for each file.
  3. You can select which coverage metric to display (Line Counters, Branch Counters etc.) by clicking on the downward-pointing triangle icon in the "Coverage" window to access the EclEmma menu.

Writing Tests

Complete WeatherUtilsTest.java by writing an appropriate set of testing methods. You should be able to check your code coverage by using the EclEmma tool.

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.