The goal of this lab is to gain experience using code coverage tools and writing high-quality JUnit tests.
JMU has the following severe weather cancellation policy*.
If any of the following conditions are met, campus will be closed:
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:
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.
Once you have set up an Eclipse project containing
WeatherUtils.java
, create a stub JUnit test class:
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
.
@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(); }
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.
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.
WeatherUtilsTest.java
file into
the new project, and make sure that JUnit 4 is on the project build
path.