JUnit Testing
The TA will walk through project setup (Getting Started) at the beginning of the recitation.
-
Create a new project and import W12L1-starter.tar. This is the same process we use to import jar files.
-
Set the
test
directory as a source folder-
Right click the
test
directory in you project folder -
Hover over the
buildpath
option in the context menu and selectUse as Source Folder
option
-
Your directory structure should now look like the following:
-
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
:
As with last recitation you will be debugging the MineSweeper
code in the BoardModel
class.
However, instead of using the debugger and code inspection to identify errors you
will be building a testing framework to alert you to problems in the code.
You can go here for more information about the MineSweeper
code.
There are 14 different bugs in the following methods of BoardModel
:
-
layMines(int row, int col)
-
isValidMove(int row, int col)
-
explore(int row, int col)
-
unflag(int row, int col)
Links have been provided for methods that are not private
. private
methods do not generate a javadoc
and,
as you will see below, cannot be tested directly using JUnit
.
Your task will be to develop unit tests to identify as many bugs as possible.
To give you an idea of what a unit test looks like two unit tests have been provided.
-
Run the
JUnit
tests:-
Right click
TestBoardModel
-
Select
Run As
from the context menu -
Click
JUnit Test
-
A test result panel should appear on the left side of Eclipse
showing the following:
The result is displays that two tests ran, two assertions failed and no errors were caused.
-
Fix the
NullPointerException
in the constructor and rerun the unit tests.
This time the result should look like the following.
These results show that two tests ran, one test was successful and one caused a RuntimeException
.
Note
|
There are ways to ensure that instead of a RuntimeException an assertion failure would be raised. The TA can explain this on request. |
JUnit cannot test private methods directly
Notice that the unit tests we provided to test the isValidMove()
method uses the reveal()
method.
Like we mentioned above, this is because isValidMove()
is a private
method of BoardModel
class and cannot be accessed by outside classes (in our case, tested directly using unit tests).
However, since the javadoc for the reveal()
method
specifies it will only reveal a tile if the move is valid we can safely use reveal()
to test if
a move is valid. You will need to use this pattern to test other private methods like unflag()
.
-
Continue to build unit test to find the other errors. There are several other styles of asserts that may be of use.
-
A full list of assert methods.
-
A more detailed tutorial on writing JUnit tests.
-
More information about testing private methods.
This lab is attendance based, have fun!