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

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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