Java Unit Testing
We will try to understand few important aspects with Java Unit Testing. Following document will help you understand what Java Unit Testing means, why Java Unit Testing is required and how you can successfully implement Java Unit Testing for your Java applications using AppPerfect.
What is Java Unit Testing?
Java Unit testing is an indispensable means of ensuring high quality Java/J2EE application. Java Unit testing generally refers to concentrated and focused testing of each smallest logical unit of your Java/J2EE application. This unit may be a module, a package, a file, or a class. It is highly recommended that unit tests be conducted at the smallest unit of your application.
Java Unit Testing is the building block of your testing infrastructure. As you develop new "units" of your application, you should also simultaneously develop units tests for them. Java Unit Testing includes generating test classes for your source code and executing it with different parameters ensuring that your source module works fine in all possible scenarios.
While most developers recognize the importance of unit testing, often due to lack of time, unit tests are not designed and run in a comprehensive and consistent manner. To save time, many developers tend to run unit tests in some form of automated manner. In fact, many users use advanced tools to help with generation and execution of unit tests. An Automated Unit Testing System performs the following key functions :
- Automatically generate unit tests for your source
- Automatically update unit tests as your source changes
- Automatically execute the tests when asked for
Using an Automated Unit Testing system can help developers save significant amount of time that would otherwise be spent on manually creating and running the tests.
Java Unit Testing with AppPerfect
Automated Unit testing of Java/J2EE applications can be implemented using AppPerfect Java Unit Test. AppPerfect Java Unit Test is a complete unit testing system. It automates the generation and execution of unit tests. AppPerfect Java Unit Test goes beyond generating simple stub classes for JUnit testing. It generates fully functional, highly intelligent test classes that understand your application and test your source files thoroughly. AppPerfect Java Unit Test also provides complete code coverage data after test execution. This enables you to view the code path taken by the test case and adjust the test case as needed. With AppPerfect Java Unit Test the efforts you, as a programmer, have to put into Java Unit Testing is greatly reduced.
AppPerfect Java Unit Test uses JUnit framework, the most commonly used framework, to generate and test Java source. To unit test server-side Java source (JSP/Servlets/EJB) it uses HtmlUnit framework. AppPerfect Java Unit Test is found to be most affordable, cost effective, efficient, reliable and accurate solution by its customers. We will now try to understand how you can successfully implement Java Unit Testing for your Java application using AppPerfect Java Unit Test.
Setting up your Java Application for Unit Testing :
The first step in unit testing is to setup a New Java Unit Test Project. Follow the steps below to create a new project.
- Start AppPerfect Java Unit Test product and select File -> New.. menu option to create a New Java Unit Test project. For details on New Project creation refer to Creating a New Project chapter. For details on configuring project properties refer to Setting Project Properties chapter.
- Provide the Name of the project and select the "Source" tab to configure source code to Unit test. In this tab you need to configure the source settings for your application. You can select to add complete source folder for Unit Testing or can add selected files.
- Select the "Environment" tab to configure classpath for your source files. In this tab you need to provide the jars and classes which are required to build your application. Once you are done providing the classpath, click on Verify Classpath button to verify that classpath is correct.
- Select the "Target" tab to configure your target application for unit testing. You can either unit test Server/Web application or can choose to unit test Load/Desktop application. In this tab you need to configure the Run Configuration for target application. Server applications are typically applications that are deployed in a J2EE application server such as BEA WebLogic, IBM WebSphere, JBoss, Apache Tomcat, Oracle Server, SunOne etc. Local/desktop applications typically run on your local machine as a single user system. For details refer to Configuring Target Application Settings chapter.
Support for Unit Testing JSP, EJB, Servlets and Struts based Web Applications :
AppPerfect's Unit Testing tool provide support for JSP, EJB, Servlets and Struts based Web Applications. It uses HtmlUnit implementation by providing browser-based test recording for JSP applications which provides in-container testing without the complexity involved with setting with an in-container environment. It also provides support for templates based frameworks such as Apache Struts. For sophisticated enterprise applications using EJBs, AppPerfect Java Unit Tester provides support for in-container testing. To enable Unit Testing for Web applications select Project -> Properties.. menu option and select the Application Type which best suits your Web Application framework. Also provide the Document root for your web application. Document Root is the root directory of your web application. Its basically the location of WEB-INF folder in your web application.
Automated Test Class Generation:
AppPerfect Java Unit Test automatically generates full test cases, by going through code and understanding it. AppPerfect Java Unit Test goes beyond generating simple stub classes for JUnit testing. It generates fully functional, highly intelligent test classes that understand your application and test your source files thoroughly. It also provides easy to use UI to edit your test cases. It automatically maintains these test cases as your code changes, so if you delete any method in your source, AppPerfect Java Unit Test will take care of deleting the corresponding test cases from test class files and so on.
Once a project is defined, the next step is to generate the necessary unit test cases. AppPerfect Java Unit Test automates the generation of such unit test cases. For .java files, the JUnit framework is used. For .jsp files, the HtmlUnit framework is used. Launch Test Generation Dialog by clicking on Project -> Generate Test Cases... menu option. Select the Test Classes tab and click on Start Generation button. Application will generate Test Cases for selected classes automatically.
Once test classes are generated using your source files, you will have test cases for all the methods as well as object instances for all objects in your project. You can go though all the generated Test Classes by selecting the Test Classes tab in the Editor view. For details refer to Generating Test Cases chapter.
Functional Test Cases Generation:
Ordinary test cases perform static analysis of source code to determine parameter values to be passed to underlying method. They also pass combination of other values as parameter. On the other hand Functional Test Cases pass the recorded values as parameters. It is necessary to perform recording in order to generate functional test cases.
AppPerfect Java Unit Test provides a utility called Object Tracer which allows users to run their application normally and monitors the object creation during the normal working of application. It records this information and later creates test cases by creating exactly same objects, this way user can generate functional test cases for their applications and can use these test cases for regression testing. You can generate Functional Test cases using Project -> Generate Functional Test Cases... menu option. For details refer to Generating Functional Test Cases chapter.
Support for Object Repository:
AppPerfect Java Unit Test extensively uses the concept of Mock Objects for unit testing. Mock Objects generally refer to an object instance that is designed to represent a real instance of the object. Since the real instance most often requires data from the user or database, which is not possible while unit testing the target source code, the instance generated by AppPerfect is called a Mock Object. Creating object instances that are as close to the real instance as possible is critical in designing high quality unit tests. Well designed mock objects improve code coverage and lead to better quality of unit tests.
In AppPerfect, all mock objects needed to unit test your project are stored in the Object Repository. The Object Repository holds all the information needed to create the object instance. The Repository is generated as part of the Test Generation process. Once Test Generation process is finished, you can see the Object Repository in the 'Repository Manager'. To access Object Repository select the Project -> Repository Manager... menu option. You can make changes to the Repository to add packages, classes and instances as per your requirements. You can also regenerate the repository, over-writing all your changes to it, to be able to start from scratch. For details refer to Object Repository chapter.
Mock Database support:
AppPerfect Java Unit Test supports mock database functionality. A mock database replaces the actual database during unit testing. Most often, users do not have access to the database layer during testing. But without a database, most database dependent applications will fail to execute. The mock database solves this issue. Mock Database handles following concerns:
- Allowing users to test their code during development phase without actually going to original database.
- During development some times users enter junk data hence to prevent original database from getting corrupt.
- To save developers time as during development if users enter junk data and it goes to original database then users are required to spend some time cleaning database.
- At lot of time database must be in some state for testing certain piece of code hence to maintain that state of database for users.
Unit Tester provides different ways to configure mock objects in order to adequately unit test the code that uses databases. You can configure to use in-memory database and populate it using a schema file, or from a CSF file. You can choose to connect to an external Database or can write your own implementation on how the JDBC calls should be handled. To configure Mock database options select Project -> Properties... menu option and select the "JDBC Expert" section in Test Case Generation tab.
AppPerfect Java Unit Test supports stub code generation, this is useful when some part of application is not available or is not developed yet. For example your application requires a database connection but same is not available then you can stub getConnection call and return some dummy connection object in place. AppPerfect Unit Test will take care to execute your stubbed code in place of actual getConnection call while unit testing the code.
By default application generates the Stubs when you run the Test Class Generation process. You can control the Stub generation by selecting Project -> Properties... menu option and configuring the option to Generate Stubs in Test Case Generation tab.
Unit Test Execution with Informative and User Friendly Results:
Once Test cases are generated, you can execute them by selecting the Project -> Run Menu Option. AppPerfect executes the Test cases and generates detailed results for Analysis. AppPerfect Java Unit Test cases provide high level of code coverage out of the box. AppPerfect Java Unit Test provides reports that help you pin point problems in your code. These reports are available through the UI and can be exported into various formats like HTML, PDF, CSV, XLS, XML etc. To export reports to different formats just right click on the view on right hand side and select the option to Export from the popup menu. For understanding Unit testing results refer to Analyzing Results chapter.
Java Unit Testing can Run Unattended in Automated Manner:
With AppPerfect's Unit Testing solution you can run lengthy unit tests unattended. Unit Tests can be scheduled to run overnight and developers can analyze the results of execution next morning. As a result you save time and improve efficiency and can concentrate on other important tasks while scheduled reviews run in background. To schedule Unit Testing you require AppPerfect Test Manager product. Once you have Test Manager installed you need to first configure Java Unit Test to enable connection to Test Manager server. To configure the Test Manager connection select Tools -> Options -> Server Connections -> Test Manager Settings and provide the host:port information for the machine where Test Manager is installed.
Once you have connection configured you can save the Java Unit Test Project to Test Manager using File -> Save in Test Manager menu option. Once test is saved to Test Manager you can schedule Test Execution using Tools -> Schedule Project menu option.
For details on creating and executing Test Schedules using Test Manager Web-UI refer to Schedules chapter.
Java Unit Testing with Team Sharing Support:
Test Engineers can share the Unit Testing results with different members in the team. Unit Testing results can be shared with developers which they can use to track the coding issues in application. AppPerfect supports integration with Subversion (SVN) version control system. For details on integration with Subversion server refer to Team Server Configuration chapter.
IDE Integration Support:
IDE Integration is a highly useful feature in AppPerfect products. Once product is integrated in IDE you can start IDE and can execute Java Unit Testing for your Java / J2EE source from within IDE. You can integrate AppPerfect applications with Eclipse, IBM Rational Application Developer (RAD), NetBeans, IntelliJ Idea, JBuilder, Oracle JDeveloper and BEA Workshop studio.
Select Tools -> IDE Integration menu option to integrate with any of the supported IDEs. For details on integration with IDE refer to IDE Integration chapter.
Integrate Java Unit Testing with your Daily Build Process with ANT Script and Command line Support:
AppPerfect supports integration with build process smoothly. You just need to export Java Unit Testing project as ANT Script or Command Line script and then you can execute these exported scripts from your build process. This way you can ensure that each build is unit tested before final deployment. To Export project as Ant Script select Tools -> Export Project as ANT Script... menu option. For details on ANT script execution refer to ANT Script Execution chapter.
AppPerfect Java Unit Test is a complete unit testing system. It automates the generation and execution of unit tests. AppPerfect Java Unit Test goes beyond generating simple stub classes for JUnit testing. It generates fully functional, highly intelligent test classes that understand your application and test your source files thoroughly. AppPerfect Java Unit Test also provides complete code coverage data after test execution. This enables you to view the code path taken by the test case and adjust the test case as needed. With AppPerfect Java Unit Test the efforts you, as a programmer, have to put into Java Unit Testing is greatly reduced. . AppPerfect Java Unit Test is found to be most affordable, cost effective, efficient, reliable and accurate unit testing solution by its customers. In case you have not yet tried AppPerfect Java Unit Test, Download Now and give it a try today.