Creating related objects

For some tests, you may need an object that is related to a given object, such as an Address that is related to a UserContact. There are two different ways the related object can be created and associated with the primary object.

Creating related objects using nested builders

Some test data builders have withX methods that take a nested builder as a parameter. For example, on the UserContactBuilder class, the withAddress builder method has a signature that takes an AddressBuilder. This is demonstrated in the following code example.

gw.transaction.Transaction.runWithNewBundle( \ bundleObj -> {
	var testUser = new UserContactBuilder()
	    .withFirstName("Tom")
	    .withLastName("Thompson")
	    .withPrimaryAddress(new AddressBuilder()
	        .withAddressLine1("123 Main Street")
	        .withCity("San Francisco")
	        .withState(State.TC_CA)
	        .withPostalCode("94110")
	        .withCountry(Country.TC_US)
	        .asBusinessAddress()
	        .create(bundleObj))
	    .create(bundleObj)
} , "su" )

Note that the primary address is specified using a nested AddressBuilder. To make this clear, the AddressBuilder code uses a second level of indentation.

Creating related objects manually

Some builders have withX methods that take an object as a parameter. For example, on the UserContactBuilder class, the withAddress builder method takes has a signature that takes an Address object. This is demonstrated in the following code example.

gw.transaction.Transaction.runWithNewBundle( \ bundleObj -> {
  var testBusinessAddress = new AddressBuilder()
      .withAddressLine1("123 Main Street")
      .withCity("San Francisco")
      .withState(State.TC_CA)
      .withPostalCode("94110")
      .withCountry(Country.TC_US)
      .asBusinessAddress()
      .create(bundleObj)
  var testUser = new UserContactBuilder()
      .withFirstName("Tom")
      .withLastName("Thompson")
      .withPrimaryAddress(testBusinessAddress)
      .create(bundleObj)
} , "su" )