Exemplars can reference other named Exemplars as a template. This allows properties to be copied across Exemplars in order to prevent duplication when defining several similar tests. To copy properties from a named Exemplar, add the template property and give it the name of the Exemplar that you want to copy. For example:
@Exemplars(set={ @Exemplar(name="setAddress1", instance="preinit!", args={"'5a'","'AB1234'"}, expect="!=(this.address,null)"), @Exemplar(template="setAddress1", args={"'10'","null"}) }) public void setAddress(String houseNumber, String postalCode) { PostalCodeService postalCodeService = new PostalCodeService(); address = postalCodeService.getAddress(houseNumber, postalCode); if (address == null) { address = new Address(houseNumber, "-", "-", "-", postalCode, "-"); } }
In this example, the second exemplar uses the first as a template, and defines different args. The instance and expect properties from the first Exemplar (setAddress1) will be copied to the second.
Unlike referencing named instances, Exemplars using a template must be in the same source file as the Exemplar the template property references.
All properties in the template Exemplar will be copied with the exception of name and description.
You can prevent any specific properties from being copied from the template Exemplar by defining a property with an empty string or empty array. For example, here we prevent the expect property from being copied from the templated Exemplar, i.e. we expect nothing:
@Exemplars(set={ @Exemplar(name="setAddress1", instance="preinit!", args={"'5a'","'AB1234'"}, expect="!=(this.address,null)"), @Exemplar(template="setAddress1", args={"'10'","null"}, expect="") }) public void setAddress(String houseNumber, String postalCode) { ...
Likewise, any property you define in your Exemplar will override a property defined in the template Exemplar.
If necessary, you can specify multiple template Exemplars by providing a list of Exemplar names to use as templates. If you do this, properties from each template will override those from subsequently defined templates. For example, properties defined in the first given template will override those defined in the second.
Consider using shorthand properties and templates to keep your Exemplars concise. For example:
@Exemplars(set={ @Exemplar(n="setAddress1", i="preinit!", a={"'5a'","'AB1234'"}, e="!=(this.address,null)"), @Exemplar(t="setAddress1", a={"'10'","null"}) }) public void setAddress(String houseNumber, String postalCode) { ...