Builder create methods
The DataBuilder class provides the following create methods:
builderObject.create( bundle )
builderObject.create()
builderObject.createAndCommit()
The following list describes these create methods.
|
Method |
Description |
|---|---|
|
|
Creates an instance of this builder's entity type in the default bundle. This method does not commit the bundle. Studio resets the default bundle before every test class and method. |
|
|
Creates an instance of this builder’s entity type in the default bundle, and performs a commit of that default bundle. |
|
|
Creates an instance of this builder’s entity type, with values determined by prior calls to the entity. The bundle parameter sets the bundle to use while creating this builder instance. |
The no-argument create method
The no-argument create method uses a default bundle that all the builders share. This is adequate for most test purposes. However, as all objects created this way share the same bundle, committing the bundle on just one of the created objects commits all of the objects to the database. This also makes them available to the PolicyCenter interface portion of a test. For example:
var address = new AddressBuilder()
.withCity( "Springfield" )
.asHomeAddress()
.create()
new PersonBuilder()
.withFirstName("Sean")
.withLastName("Daniels")
.withPrimaryAddress(address)
.create()
address.Bundle.commit()
In this example, Address and Person share a bundle, so committing
address.Bundle also stores Person in the database. If you do not need a
reference to the Person, then you do not need to store it in a variable.
GUnit resets the default bundle before every test class and method.
The create and commit method
The createAndCommit method is similar to the create method. However, after adding the entity to the default bundle, this method commits that bundle to the database.
The create with bundle method
If you need to work with a specific bundle, use the create(bundle) method. Guidewire recommends that you use this method inside of a transaction block. A transaction block provides the following:
- It creates the bundle at the same time as it creates the new builder.
- It automatically commits the bundle as it exits.
The following example illustrates the use of a data builder inside a transaction block.
uses gw.transaction.Transaction
function myTest() {
var person : Person
Transaction.runWithNewBundle( \ bundle -> {
person = new PersonBuilder()
.withFirstName( "John" )
.withLastName( "Doe" )
.withPrimaryAddress( new AddressBuilder()
.withCity( "Springfield" )
.asHomeAddress() )
.create( bundle )
} )
assertEquals( "Doe", person.LastName )
}
Notice the following about this example:
- The example declares the
personvariable outside the transaction block, making it accessible elsewhere in the method. - The data builder uses an AddressBuilder object nested inside
PersonBuilderto build the address. - The
Transaction.runWithNewBundlestatement creates the bundle and automatically commits it after Gosu Runtime executes the supplied code block.
In summary, the create(bundle) method does not create a bundle. Rather, it uses the bundle passed into it. Guidewire recommends that you use this method inside a transaction block that both creates the bundle and commits it automatically.
If you do not use this method inside a transaction block that automatically commits a bundle, then you must
commit the bundle yourself. To do so, add bundle.commit to your code.
