Overview of test data builders
Tests that verify application functionality often require objects to test against. For example, suppose you want to write a test that verifies the logic for assigning an activity to a user in the correct group. In order to test this logic, you must have at least one activity, one user, and one group. Furthermore, the state of these objects must be appropriate for the test. For example, to test activity assignment, you might need to start with an activity that is unassigned.
Theoretically, you could execute these tests using the approach taken to create objects in a production environment. Within a test framework, this approach tends to be cumbersome for several reasons:
- Objects often have fields that are required at the database level. In a production environment, values must be specified for these fields. However, within a test environment, many of these values may be irrelevant to a given test. Setting these values increases the code length and can make the code harder to read.
- Objects may have values that are cumbersome to set, such as:
- A given object may require a parent object.
- A given field may need to have a value that is unique
- Objects in a production environment are tied to automatic application behaviors, such as pre-update rules or messaging events. These behaviors can be irrelevant to testing and may throw errors for reasons irrelevant to testing.
To simplify object creation for testing purposes, Guidewire provides test data builder. A test data builder is a class that can be used to build objects for testing purposes. Test data builders provide the following benefits:
- All required fields have associated default values. When you create an object from a test data builder, you only need to specify values for the fields relevant to the test. The class provides any required values not specified by your code.
- Helper methods exist to simply the setting of more cumbersome values, such as:
- Required associated objects
- Fields whose values must be unique
- Objects created from test data builders do not trigger automatic application behaviors, such as pre-update rules.
For example, the following code uses a test data builder to create a test user object and then print values from that object. Note that the FirstName value is specified in the code, but the LastName field is not. When the test user object is created, the FirstName field is set as specified, and the LastName field is given a default value.
var testUser = new UserContactBuilder()
.withFirstName("Tom")
.create()
print(testUser.FirstName)
print(testUser.LastName)
OUTPUT:
Tom
House 0006
Test data builder classes are also referred to as builders, data builders, and entity builders.
The base configuration provides test data builders for most frequently-used entities. They
are declared in the gw.api.databuilder package. If necessary, insurers can
extend these test data builders. They can create their own test data builders. For more
information, see Creating and extending test data builders.
Test data builders focus only on fields required by the database
Test data builders are designed to create objects that have enough data to be saved to the database. There may be circumstances where this is insufficient to meet the needs of a given test.
For example, suppose there is a test that requires an Account object to be shown in a specific user interface screen. The screen has the Email field marked as required, even though this field is not required in the database. If you create an Account object using a test data builder and do not provide an Email value, an exception may be thrown when the object is tested in the user interface.
Similarly, suppose there is a test for Note objects that requires the Topic field to have a value. This field is not required at the database level and is normally set by preupdate rules. Test data builders do not trigger preupdate rules. Therefore, if you create a Note object using a test data builder and do not manually provide a value for the Topic field , the test will fail.
When writing code that uses test data builders, keep in mind that each use case may require data beyond what is strictly needed to save the object to the database. In these cases, you must write code the provides values for these additional requirements.
Test data builders are not supported in production environments
Test data builders are available only in non-production environments. Objects creates from test data builder classes bypass many application constraints and are therefore not appropriate for a production environment.
To enable test data builders, you must set the Enable Internal Debug Tools configuration
parameter in config.xml to true.
