Reference no: EM133673724
Task: Unit test and code coverage
Overview
Code coverage is a measure of the percentage of code that your unit tests execute. Emma is a code coverage tool that does the dirty work of taking the measurements. Emma can be installed as Eclipse IDE plugin. Part A: You will need to write JUnit test cases functions in WeatherAndMathUtils.java to achieve above 90% code coverage. Part B: you will need to investigate code coverage for (a) conditional loop and (b) conditional loop containing a conditional statement.
Submission details
Use the instructions on the following page to carry out this task's steps.
For this task you will need to study the existing project task6_1P.zip which contains WeatherAndMathUtils.java and a test file WeatherAndMathUtilsTest.java. You need to update the test java file as required to achieve above 90% code coverage.
Submit a pdf combining below items for both the parts (part A & B)
A screenshot of your Eclipse IDE's (i) JUnit tab which shows test statistics including Runs, Errors and Failures and (ii) Emma coverage output which shows coverage statistics including percentage coverage per java file and corresponding number of instructions covered/missed.
Your program's source code for tests WeatherAndMathUtilsTest.java
A screenshot of your GitHub page where your latest project folder is pushed.
You want to focus on the following key ideas, and make sure you can explain them in relation to your program.
Code coverage and its significance and limitations
Path analysis of source code such as statement, conditional statement, conditional loop.
Instructions
For this task you will need to
Download task6_1P.zip Java project and unzip it in a common folder (say, java_projects) which you will be using to store all the weekly projects.
Import the project (as a maven project) in Eclipse IDE.
Observe WeatherAndMathUtils.java file contains function weatherAdvice(double windSpeed, double precipitation) and two math functions isEven(num) and isPrime(num). The weatherAdvice function advice alert levels for different weather severity such as a dangerous level of alert if
More than 6.0 inches of rain are expected.
Wind speeds greater than 70.0 mph are predicted.
There will be over 4.0 inches of rain and wind speeds will be above 45.0 mph.
If none of the above conditions are met, a warning level alert is issued if any of the following conditions are met:
Predicted wind speeds are above 45.0 mph.
Predicted precipitation is above 4.0 inches.
An allclear alert is generated for the rest of the normal conditions.
A test file WeatherAndMathUtilsTest.java contains a function to test a dangerous weather alert advice for 70.1 mph windspeed which is slightly above the maximum windspeed threshold. This file also contains a few other unsuccessful test functions. You can run this test file with Emma code coverage command in Eclipse (rightclick test file > Coverage As > JUnit Test). Emma codecoverage plugin shows statistics and highlights both the test and source files. Figure below shows the highlighted weatherAdvice function body for codecoverage and statistics in Coverage console. The statistics show that the source package (src/main/java) has a total of 75 lines of code among which 19 statements were covered or tested by the test file and 56 remaining statements are yet to test. The statistics also show the coverage of the test file which shows among 24 instructions, 11 test instructions failed which you can see there are 3 tests which are failing.
Regarding part B, create another java file in the same package as WeatherAndMathUtilsTest.java file and write 2 functions: (a) the first function should contain a conditional loop (could be a for loop or while loop, if supported by the programming language) with simple instructions in the loop body and (b) the second function should contain a conditional loop with a conditional statement in the loop body. You need to create tests to achieve above 90% code coverage on these 2 functions.
There are 2 test failures that you need to correct by providing your name and id in the first 2 test cases.
You need to create more test cases and run the code coverage to improve the coverage above 90% in the source code WeatherAndMathUtils.java file and in the part B java file.
Run the test, take screenshot of code coverage statistic.
Upload your folder to your GitHub account and take a screenshot.
Your Task
Your task is to:
Study the functions in WeatherAndMathUtils.java (there are 3 of them) and a passed test function in WeatherAndMathUtilsTest.java test file.
Run the test file Coverage As > JUnit Test to see the coverage output and code highlight.
Add more test cases to achieve above 90% code coverage at the source java file.
Repeat above testing and code coverage steps for part B but for that you also need to create two separate functions satisfying both the conditions of part B - (a) a loop with simple statement and (b) a loop with conditional statement.
Make sure all tests run so 100% coverage of test codes are obtained. In case your tests can not cover 100% coverage, justify the reason why your tests failed to do so and how to mitigate the problem.