Java Reference
In-Depth Information
eventually run with the project's own code and can then try to determine why the ex-
ception was thrown.
Note
The Stack trace output will contain line number information if the program is
compiled with “Debug” info. By default, most IDEs will include this information when
running in a Debug configuration.
11-2: Locking Down Behavior of Your
Classes
Problem
You need to lock down the behavior of your class and want to create unit tests that will
be used to verify the specific behavior in your application.
Solution
Use JUnit to create unit tests that verify behavior in your classes. To use this solution,
you need to include the JUnit dependencies in your class path. JUnit can be down-
xxx.jar
and
junit-xxx-dep.jar
files to your project or class path (where
xxx
is the downloaded version number). When JUnit becomes part of your project, you will
be able to include the
org.junit
and
junit.framework
namespaces.
In this example two unit tests are created for the
MathAdder
class. The
MathAdder
class contains two methods:
addNumber (int, int)
and
sub-
stractNumber (int,int)
. These two methods return the addition (or subtrac-
tion) of their passed parameters (a simple class). The unit tests (marked by the
@Test
annotation) verify that the
MathAdder
class does, in fact, add and/or subtract two
numbers.
package org.java8recipes.chapter11;
import junit.framework.Assert;
import org.junit.Test;