Browse DevX
Sign up for e-mail newsletters from DevX


JUnit + Jtest = Automated Test Case Design and Static Analysis : Page 3

Jtest, an automated unit testing and static analysis tool, extends JUnit to provide automatic test case generation and static analysis functionality.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.

A Runtime Exception that Jtest Exposed
Figure 3: A Runtime Exception that Jtest Exposed

The violation of the PB.TLS static analysis rule shown in Figure 4 reveals that the developer of this class inadvertently wrote case10 instead of case 10. If the class is not fixed, it will give incorrect results when it is passed the value 10. If this problem is allowed to remain in the code, it will likely cause strange, difficult-to-diagnose errors during application testing or in the field. Once again, by identifying and fixing this problem immediately, you can prevent grief later.

A Coding Standard Violation that Jtest Identified
Figure 4: A Coding Standard Violation that Jtest Identified

Create a JUnit-compatible Test Class for Automatically Generated Test Cases
Finally, assume that you want to create a JUnit test class that represents the 15 automatically generated test cases. You choose Jtest's Export to a JUnit Test Class option. Jtest instantly generates a test class that allows you to run these 15 test cases in either Jtest or JUnit. You can add more functional test cases by modifying the test methods in the automatically generated test class, and then you can run the modified test class in either Jtest or JUnit. Adding test cases in this manner enables you to verify whether the class is correct. This strategy significantly reduces the amount of code that you need to write for each test—without sacrificing test quality.

Integrate Unit Testing into Daily Development
These days, almost every Java developer recognizes the value of practices such as unit testing and coding standard enforcement, and intends to perform them as part of his or her regular testing practices. However, these practices can be time-consuming, and most developers rarely have the opportunity to perform them as early, frequently, or thoroughly as they would like. Extending JUnit with Jtest can solve this problem. When you use both Jtest and JUnit, you can realistically integrate unit testing and coding standard enforcement into your daily development practices.

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.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date