Introduction

UTA can use the factory methods that are available in your code to create objects during test generation. This requires tagging individual methods as factory methods in the Javadoc (see Tagging a Factory Method) to enable UTA to discover them, and to use them to initialize objects when tests are created.

You can configure UTA to automatically discover the factory methods that you tagged, or manually instruct UTA to search your project(s) for these methods (see Scanning for Factory Methods).

Tagging a Factory Method

You can tag a public static method in a public non-inner class. The method must return a complex type that does not contain a bounded type parameter. You can tag a method in one of the following ways:

In the following example, the createMyObject method is tagged:

public class MyObjectFactory {
    /**
     * @jtest.factory
     */
    public static MyObject createMyObject(int param) {
        return new MyObject(param);
    }
}

If the method is not tagged, UTA will not use it during test creation, and myObject will be initialized to null:

@Test
public void testMyMethodUnderTest() throws Throwable {
    // When
    MyObject myObject = null;
    String results = myMethodUnderTest(myObject);

    // Then
    // assertEquals("", results);
}

If the method is tagged, UTA will use it to create the object in the test, and your code will look as follows:

@Test
public void testMyMethodUnderTest() throws Throwable {
    // When
    int param = 0;
    MyObject myObject = MyObjectFactory.createMyObject(param);
    String results = myMethodUnderTest(myObject);

    // Then
    // assertEquals("", results);
}

A tagged factory method must be in the same project as any tests that are going to use it.

If you want UTA to use a factory method that was tagged in another project, create a new method in the project where the tests will be generated, tag it as a factory method, and point to the external factory method you want to use:

public class TestUtil {
    /**
     * @jtest.factory
    */
    public static MyObject createMyObject() {
        return MyObjectFactory.createMyObject(3);
    }
}

Untagging Factory Methods

You can remove the @jtest.factory tag from Javadoc in one of the following ways:

Scanning for Factory Methods

You can configure UTA to automatically scan all projects for tagged factory methods by enabling the Automatically discover tagged factory methods option; see Configuring Preferences. With this option enabled, UTA will automatically scan your projects on the IDE startup, and display the discovered factory methods in the Factory Methods view:

The view will be automatically updated when you modify your project(s) (for example, when you create or save a Java file, or create or delete a new project).

Alternatively, you can manually instruct UTA to scan your code to detect tagged factory methods.

  1. If not already open, choose Parasoft> Show View> Factory Methods from the IDE menu bar.

  2. Select a project(s) in the Package Explorer.

    UTA will not scan projects selected in the Project Explorer or the Navigator.

  3. Click the Scan workspace or Scan selected projects button in the Factory Methods menu to scan a specific project(s) or the entire workspace for factory methods:


Using Factory Methods

The factory methods that are discovered by UTA and displayed in the Factory Methods view will automatically be used during test generation when you use one of the following options: