| http://www.devx.com | Printed from http://www.devx.com/Java/Article/15625/1954 |
|
JUnit + Jtest = Automated Test Case Design and Static Analysis
Jtest, an automated unit testing and static analysis tool, extends JUnit to provide automatic test case generation and static analysis functionality.
by
Carson Ellsworth and
Cynthia Dunlop
|
f you currently use JUnit for Java unit testing, you can extend it to automate test case design and coding standard enforcement with Parasoft Jtest. Jtest is an automated unit testing and static analysis tool that includes and extends JUnit to provide automatic test case generation and automatic checking of over 300 Java coding standards.
Test Case Generation Benefits Jtest's automatic test case generation helps you prevent errors in two main ways:
Static Analysis Benefits
|
| 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:
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.
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.
|
| What Jtest Caught To get an idea of the types of problems that Jtest identifies automatically, let's examine the uncaught runtime exception and one of the static analysis violations that Jtest uncovered. The uncaught runtime exception message shown in Figure 3 reveals that the startsWith method is implemented incorrectly. The method should return false for the argument "" and "0", but it throws a runtime exception instead. If the error is not fixed, any application using this class will eventually crash or give incorrect results. By identifying and fixing this problem immediately, you can prevent significant grief later.
The violation of the PB.TLS static analysis rule shown in Figure 4 reveals that the developer of this class inadvertently wrote
Create a JUnit-compatible Test Class for Automatically Generated Test Cases
Integrate Unit Testing into Daily Development
Carson Ellsworth is a Lead Systems Engineer for Parasoft Corporation as well as Java product support manager. He has extensive knowledge of Java design and testing paradigms. His professional experience includes software engineering, design, documentation, and quality assurance. You can reach him at carson@parasoft.com.
Cynthia Dunlop has been responsible for crafting Parasoft product manuals and technical papers since 1998. Dunlop has also authored numerous technical articles about issues related to software development. You can reach her at cynthia@parasoft.com.
|
| Guest Info |
| DevX is a division of Jupitermedia Corporation © Copyright 2007 Jupitermedia Corporation. All Rights Reserved. Legal Notices |