Creating objects using test data builders
The runWithNewBundle method
Within InsuranceSuite, a bundle is a set of objects that are committed to the database as a group. Bundles are analogous to database transactions. Every object is created within the context of a bundle. The object is saved to the database when the bundle is committed.
One of the most common methods used to interact with bundle is the
gw.transaction.Transaction class's
runWithNewTransaction. This method has a signature that specifies a Gosu
block and a specific user. The Gosu block names a bundle and typically executes code that
creates objects within that bundle. At the end of the block, the bundle is inherently
committed to the database. The commit is executed as the specified user.
The syntax for this usage of runWithNewTransaction is:
gw.transaction.Transaction.runWithNewBundle( \ bundleObject -> {
// code that creates objects in bundleObj
} , "userName" )
For more information on runWithNewTransaction, refer to the section on
bundles and database transactions in the Gosu Reference Guide.
Creating test objects in runWithNewBundle
Code that creates test objects typically appears within the block of a
runWithNewBundle call. The code typically has the following
structure:
- One line that declares a variable for the new object and calls the appropriate builder class
- Optionally, one or more lines that set values for the new object
- One line that creates the object
The following line of code demonstrates this three-part structure:
gw.transaction.Transaction.runWithNewBundle( \ bundleObj -> {
var testUser = new UserContactBuilder()
.withFirstName("Tom")
.create(bundleObj)
} , "su" )
- The first line calls
runWithNewBundleand names the bundlebundleObj. - The second line declares a new object whose name is
testUserand whose type isUserContactBuilder. - The third line sets the
FirstNamefield on the new object. - The fourth line creates the object in the
bundleObjbundle. - The final line ends the block, which inherently commits the bundle as the super user
su.
The following sections provide more detail on how to declare the test object variable, set its values, and create the object using those values.
