How to Extend JUnit with Jtest: A Simple Example
To better understand how Jtest can extend JUnit, review a simple example of these two tools working together. Assume you have just written Simple.class, a very basic class (see Listing 1
for its source code), and you want to verify whether it is correct and robust before you start working on another class.
If you relied only on JUnit, you might start the testing process by creating a test class like the one shown in Listing 2.
If you compile this test class then execute it using JUnit, all the test cases will pass. You probably would then check the source code into the source code repository and begin working on the next class.
However, if you use both Jtest and JUnit in this scenario, you would perform the following steps:
- Load Simple.class into the Jtest Class Testing UI.
- Click the Start tool bar button to start the test (as shown in Figure 1).
Jtest examines the class under test, statically analyzes the class's source code, designs test cases that test the class's construction and attempt to cover every branch of every method, and then executes both the automatically generated test cases and the six JUnit test cases represented in the SimpleTest test class. This entire process takes less than 25 seconds. If the class contained Design by Contract comments, Jtest would have also designed test cases that verify the class's functionality.
When the test is completed, Jtest reports the results shown in Figure 2.
|Figure 2: The Results of the Simple.class Test|
By using Jtest with JUnit, you identified 12 violations of Java coding guidelines and one uncaught runtime exception. In addition, you gained 15 new test cases that you can export as a JUnit test class and/or use for functionality testing.