JUnit Testing

The TA will walk through project setup (Getting Started) at the beginning of the recitation.

Getting Started
  1. Create a new project and import W12L1-starter.tar. This is the same process we use to import jar files.

  2. 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 select Use as Source Folder option

Your directory structure should now look like the following:

setup1
  1. Add the JUnit library to Eclipse

    • 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:

setup2
Description

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:

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.

Directions

To give you an idea of what a unit test looks like two unit tests have been provided.

  1. 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:

result1

The result is displays that two tests ran, two assertions failed and no errors were caused.

  1. Fix the NullPointerException in the constructor and rerun the unit tests.

This time the result should look like the following.

result2

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().

  1. Continue to build unit test to find the other errors. There are several other styles of asserts that may be of use.

Additional Resources
Submission

This lab is attendance based, have fun!