Using JUnit in Eclipse

JUnit is a unit testing framework for Java that helps programmers write and run automated tests. When using JUnit for testing, the programmer typically writes many small tests to automatically check that a program behaves as expected. Often, the programmer will write one or more tests for each method. For this lab, we have written one or two test for each method. Each test tries a single test case; the test invokes the method for test case and checks that the returned value is as expected.

JUnit is integrated into the lab version of Eclipse. Because you already have an implemented set of JUnit tests and skeleton code for the WordGamesUtil class, you can run the tests right away.

First we need to make JUnit available to Eclipse. Select the Project menu and click on Properties.

On the list to the left choose Java Build Path. In the tab selector, choose Libraries. Press the Add Library... button.

Choose JUnit, and press Next >.

Make sure that JUnit 4 is the JUnit library version, and press Finish.

To run the test, right click on the WordGamesUtilTest.java file in the Project Explorer panel, select Run As, and then select JUnit Test (see image below):

running junit

In the JUnit output panel, you should see the following:

running junit

Notice that some tests have a green checkmark beside them; these are the tests that passed. The tests passed because the default return values that were chosen happened to be the return values expected by the test.

Some tests have a blue x beside them, indicating that the tests have failed. The tests failed because the default return values that were not the return values expected by the test.

The tests having a red x beside them are tests that caused an exception to be thrown. These tests have failed because we are returning null from these methods, and the tester is trying to invoke a method on a null reference (which always causes an exception).

The Failure Trace tells us that the test named testIsNotIsogram failed and it provides some information as to why the test failed. We can see that the test failed because the method isIsogram returned true when given the string "unpredictable" (which is not an isogram because is has two e's).

To proceed, you need to correct the errors in all of the methods where a unit test has failed.