<< Chapter < Page Chapter >> Page >
Unit testing is a fundamental testing process in the development of object-oriented systems. The module is a primer on using the JUnit unit testing framework that is integrated into DrJava.

Object oriented systems derive great complexity through the interactions between the objects in the system. It is often impossible to test the entire range of the total complex behavior that a system is designed to exhibit. What one can say however is that if any given part of the system does not perform as desired, then one can make no assurances whatsoever about the integrity of the system as a whole. Thus the testing of these smaller parts, or "units" is a crucial element of developing any large-scale OO system. A unit test is a complete test of a small, atomic sub-system. Note that this is not the same from a partial test of some aspect of the whole system!

Unit tests are often performed at the individual class level, meaning that a test class that runs all the complete battery of tests on the tested class, is written for each class to be tested. In some cases however, certain systems of classes are not divisible into isolated, atomic units and in such must be tested as a whole. The key is to test as small a piece of the system as possible and to test it as thoroughly as possible.

Using junit in drjava

Suppose we have a class such as the following that we wish to test. Note that class is public and that the method we wish to test is also public .

Sample code to be tested

Some example code to be tested.
In DrJava, select "File/New JUnit Test Case...". Enter a name that is descriptive of the test(s) you wish to make. A recommendation is to use the class name being tested prepended with " Test_ ", such as " Test_MyClass ". This enables all your test classes to be easily identified, DrJava will then automatically create a valid JUnit test class, such as below. (Note that DrJava does not initially name the new class file, but the default name suggested by DrJava when you attempt to save it will be correct.)

Autogenerated unit test class

Test class autogenerated by DrJava.

Rename the auto-generated " testX() " method to something more descriptive of the particular test you'd like to perform. The new name must start with " test " and must return void and take no input parameters. You can create as many test methods as you wish to test your code. JUnit will automatically run all methods that begin with " test " as test methods. Typically, a single test method will test a single method on the class under test. There are situations however where a single method under test may require several test methods to properly test its full range of operation. In the code that follows, the testX() method has been renamed to " test_myMethod1() ".

Assertequals(...)

There are a number of ways in which one can test that the proper result is produced. The simplest method is to compare a single output value to an expected value. For instance, suppose you are testing a method that returns a result. One can compare the actual returned value to the value one expects for the given inputs. There are two methods supplied by the junit.framework.TestCase class, which is the superclass of the test class generated by DrJava. The first is void assertEquals(String failResponse, Object actual, Object expected) . The actual input parameter is the actual result of the method under test. The expected parameter is the expected result. failResponse is a String that JUnit/DrJava will display if the actual value does not "equal" the expected value. The assertEquals(...) method is overridden such that actual and expected values can be either Objects or primitives. assertEquals(...) uses the equals(...) method of Object to makes it determination. For primitives, the usual == is used. For instance, we could write the test case below. If the actual value does not equal the expected value, assertEquals throws a exception that is caught by the JUnit framework and an error message will result. To test the code, first compile all the code then select the test class. Right-click the test class and select "Test Current Document "or click on "Tools/Test Current Document." The following result will be displayed, given the test code we wrote to purposely fail:

Using assertequals(...) in a test case

assertEquals(...) displays the error string as well as comparative information if the actual and expected values are not equal.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask