JUnit Testing
The TA will walk through project setup (Getting Started) at the beginning of the recitation.
-
Create a new project and download BlackBoxTesting-starter.jar. Import the jar into your source directory by right clicking on the project > import > General > Archive File, then browsing to the location of the downloaded jar file, and clicking finish. Your TA will go over this process. Your eclipse directory should look like this:
L3/ └── src └── TestCases.java └── TestingFunctions.java
-
Add the
BlackBoxFunctions
library to your project.-
Download BlackBoxFunctions.jar
-
Right click the project directory
-
Hover over the
build path
item in the list shown -
Select
Configure Build Path…
-
Select
Libraries
tab -
Select
Add External JARs…
, and browse toBlackBoxFunctions.jar
-
Click
Apply
-
-
Add the
JUnit library
toEclipse
-
Right click the project directory
-
Hover over the
build path
item in the list shown -
Select
Add Libraries…
option in the sublist -
From the list show select
JUnit
, click next then finish
-
Your directory structure should now include an additional directory called JUnit
.
Develop unit test for each function defined in TestingFunctions.java
, to verify both that the basic behavior works
and that edge cases are handled properly.
The BlackBoxCorrect
class is correctly implemented, while the BlackBoxIncorrect
class has bugs in edge cases. As
such, all of your tests should pass for BlackBoxCorrect
, but some will fail with BlackBoxIncorrect
.
Implement the tests outlined in TestCases.java
, and write additional tests for each function. There are 2 bugs in
the incorrect version of greatestCommonDivisor
, and one bug in reverseWindow
. Expose all of these bugs with unit tests
for completion.
Here are some example test cases you should look for:
-
For
greatestCommonDivisor
:-
Negative, and 0 value inputs
-
Inputs with a GCD of 1
-
Inputs with a GCD equal to one of the values
-
-
For
reverseWindow
:-
Input indices out of bound of the array size
-
Input indices at the bounds of the array size
-
Reversed input indices
-
Input with an empty array
-
Input with equal indices
-
Read the javadoc for TestingFunctions
for specifications on the expected behavior of
a correct implementation.
To give you an idea of what a unit test looks like, your TA will walk thorugh the implementation of some basic tests with you.
-
Run the
JUnit
tests:-
Right click
TestCases
-
Select
Run As
from the context menu -
Click
JUnit Test
-
-
A full list of assert methods.
-
A more detailed tutorial on writing JUnit tests.
-
More information about testing private methods.
Show your TA the tests you wrote, and the output of running them on both BlackBoxCorrect
and BlackBoxIncorrect
.
You must expose three bugs for completion: 2 in greatestCommonDivisor
and 1 in reverseWindow
.