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. This chapter explains how to create, manage and maintain your Object Repository.

Structure of the Object Repository

The Object Repository stores all the data types associated with the projects selected in the AppPerfect Java Unit Test.

Object Repository is project-specific, it keeps adding objects as test cases are created in the Unit Test. It is one of the most useful features of the AppPerfect Java Unit Test as it allows you to provide various possible values for each data type your project contains. When you add a value for a data type, Unit Test automatically generates a Test Case with that value passed as the parameter. This is true for any number of values that you might add. Datatypes are categorized into three types: Java classes, Mock Classes and User Classes.

Java classes

All "core Java" classes belong to this category. A core Java class is defined as any classes whose package starts with java.* or javax.*. AppPerfect understands most core Java classes and can instantiate them properly. Examples are String, fonts, colors, date, points, etc. AppPerfect can also handle all collection classes (i.e. properly instantiate them) such as vectors, arrays, lists, etc.

A special sub-category of Java classes is Primitive datatypes: boolean, byte, char, double, float, int, long and short. Full support is provided for primitive data types. Sample objects are created by default for each data type. You can edit existing objects or create more objects of this type in the Repository.

Mock classes

If any Java interface or abstract class is included in your project for unit testing, and no corresponding implementing class is found, AppPerfect will create a mock implementation of that interface or abstract class and place it in this category.

User classes

Any class not belonging to the above two falls into this category. Hence, you may find not only your classes listed here, but any third party libraries that you may have used.

Actions on Repository

Generate and Edit the Repository

The Repository is generated as part of the Test Generation process. Once Test Generation process is finished, you can see the test classes in the Test Classes tab and the Object Repository in the 'Repository Manager'. 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.

Exporting and Importing the Repository

You can export the Object Repository from the repository manager. The object repository for your project can be exported and saved as an XML file.

You can re-use the exported object repository files for any of your other projects by using the import functionality provided in repository manager.

When an existing Object Repository file is imported, it will create all the objects in the Object Repository that do not already exist in the repository. There may also be some conflicts - that is, repetition of items in the existing repository and the one being imported. You will be asked for your preference in case of conflicts at the time of importing the repository. You can choose to replace the existing object instance with the new instance from the imported Repository, or you can stay with the existing object instance by ignoring the one from the Repository being imported.

Actions on Classes

You can view all the data types listed in the Object Repository in a tree structure. There are three root nodes viz. Java Classes, User Classes and The Java Classes contains the Primitive Datatypes and various other java packages. You cannot add or delete any Primitive Datatypes as they are pre-defined and fixed in number.

Adding Packages

You can add only java packages to the Java Classes node by using the right click on the Java Classes node and selecting Add Package. The Input dialog box is thereafter shown and you can enter the name of the package in the text field and click ok. If the package is a java package it will be added to the Java Classes otherwise it will by default be added to the User Classes node. Clicking any of the packages from the Details panel shows the classes that are present in the package.

Deleting Packages

When you select the Java Classes, User Classes or the Mock Classes from the Repository Tree, the Package Name and Number of Classes in each package are shown in the Package Details panel on the right. Select the package you want to delete by clicking Delete Package after selecting the corresponding Number of Classes column for the Package Name.

Adding Classes

Similarly, the classes can be added to the packages that are defined by using the right click and selecting Add Class. You can also click on Add from the details panel of the object repository after selecting the package in which you want to add a class. The class name has to be first entered in the Input dialog box and is then added to the selected package. Clicking on any of the classes from the Details panel shows the instances of the class.

Deleting Classes

When you select the package from the Repository Tree, the Class Name and Number of Instances of each class are shown in the Class Details panel on the right. Select the class you want to delete by clicking Delete after selecting the corresponding Number of Instances column for the Class Name.

Actions on Instances

Adding Instances

When you click a class name in the repository, you can see the details of the instances of the class in the view on the right. The details are given in a table, which lists the Instance Name, Value, Parameter Class, Parameter Value, Value Type and Description. Each of the instances of the class are listed in a tree like structure with the parameter classes that belong to it. Click Add Instance to add instances to the class. This opens the Add/Edit New Instance dialog box.

Add/Edit New Instance

Class Name: The class name is specified in this field, which cannot be modified, as the class is already defined.

Instance Name: Specify the name for the instance of the class that you want to add.

Constructors: This is a drop-down box with the list of constructors of the selected class.

Description: The text you enter in this field will be shown in the Description column in the Details panel of the Class.

Parameter Class: Depending on the Constructor selected, the appropriate parameter classes are listed in the table.

Value: The various values for each of the parameter classes are listed in a drop-down box corresponding to the parameter class.

NOTE: When adding values for Interfaces, please make sure you create an instance of a class that implements the interface as you cannot create objects of an interface directly..

Editing an Instance

When you click Edit Instance the Add/Edit New Instance dialog is displayed.

Class Name: The class name is specified in this field, which cannot be modified, as the class is already defined.

Instance Name: The name of the instance that you want to edit is displayed and cannot be modified.

Constructors: Shows the constructor of the selected class and is not modifiable.

Description: The text entered in this field can be modified and will be shown in the Description column in the Details panel of the Class.

Parameter Class: Depending on the Constructor selected, the appropriate parameter classes are listed in the table.

Value: The various values for each of the parameter classes are listed and can be modified using the drop-down box corresponding to the parameter class.

Deleting an Instance

Select the Instance you want to delete and click Delete Instance to delete the selected instance from a particular class.

Find

You can look for nodes by their names using the Find dialog.