Exercise 3
info
This is an in-class exercise. An exercise page like this one will contain a brief description but is intended to be supplemented by discussion during our meeting time. Complete the exercise to the best of your ability in the time given. Feel free to talk with other students as you work, and do not be afraid to ask questions. Aim to complete as much as possible during our meeting, but you need not hand it in.
#
Learning ObjectivesObjectives
This exercise should help you:
- Whitebox testing techniques:
- Method coverage,
- Statement coverage,
- Branch coverage
- More practice with implementing test cases in JUnit
#
Task 1Consider this implementation of countOutsideRange
method:
Now, answer the following questions:
- Write test case(s) that achieve (if not possible to achieve any of these coverages, explain.):
- method coverage
- statement coverage but not branch coverage
- branch coverage but not path coverage
- full path coverage
- Which of the abovementioned coverage criteria are good enough to reveal all faults (if any)?
- Is there any faults that the test cases suggested above cannot reveal?
info
You do not have to implement these test cases in JUnit, but if you like more practice with Java, feel free to implement your test cases.
#
Task 2Do a git pull on the course reporsitory to get a copy of ExpUtils.java
file under exercises\ex3
folder. There is one method in this class validBrackets(String)
which you will be testing in this part.
#
Design test casesUsing pen and paper only:
- Write test cases to achieve full branch coverage.
- Are any faults revealed using the test cases that achieve full branch coverage?
- Can you point out any other fault(s) that is/are not necessarily detectable by acheiving 100% branch coverage?
- Add test cases (if needed) to satisfy "Loop boundary adequacy".
tip
To asnwer question 3 above, read the Javadoc specs carefully and put into work your blackbox testing skills!
#
ImplementationNow, do the followings:
- Implement in JUnit the test cases that you designed in the previous part.
- Utilize a code coverage tool (e.g. Intellij Code Coverage, Jacoco etc.) to run your test cases, measure code coverage, and verify that your test cases indeed achieve 100% branch coverage.
Jacoco on Gradle project in Intellij
In order to add Jacoco to your Gradle project in Intellij, all you need to do is to add the following to your build.gradle
under plugins
:
Also add the following directly in build.gradle
:
This specifies that tests must be run before generating coverage report and also mandates generation of report in thee different formats namely html
, csv
and xml
.
Then, you need to type the following (in a terminal) to run tests and generate code coverage report:
#
Fix the faultsFinally, fix all the faults that you have found, run the test cases and verify you have a green test suite.
Here is the specs and implementation of ExpUtils
for your reference: