Creating Test Cases
To test this class, you'll need to create a test case for it. A test case is a class that extends the JUnit TestCase class and contains test methods that exercise your code. To create a test case, right-click on Factorial.java in the Package Explorer and select New →
JUnit Test Case.
Tip
If you get a dialog offering to add the JUnit library to the build path, select Yes.
A dialog window will come up with the name of the test case (FactorialTest) already filled in, along with the name of the class being tested. Click Next to show the Test Methods dialog, select the factorial(int) method, and click Finish to generate the test case. Eclipse will then generate some code for you, similar to the following:
public classFactorialTestextendsTestCase {public voidtestFactorial() { } }
Now, all you need to do is supply the contents of the testFactorial() method. JUnit provides a number of static methods that you call in your tests to make assertions about your program's behavior. See Table 5 for a list.
Table 5-1. JUnit assertion methods
|
Method |
Description |
|---|---|
|
|
See if two objects or primitives have the same value. |
|
| |
|
|
See if two objects are the same object. |
|
| |
|
|
Test a Boolean expression. |
|
| |
|
|
Test for a null object. |
|
|
To test the factorial() method, call the method with a few sample values and make sure it returns the right results. Now, insert a blank ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access