Test Model Assembly
Developers can use the @TestModelAssembly
annotation to have more control over the test model build process.
The annotation can provide a template, which constructs a complete model spec from the spec returned by the annotated
method.
@TestModel()
@TestModelAssembly(
// Optionally, provide the default programType.
programType = "elements::JeeApplication",
// Provide a template class which adds context to the model spec.
template = ComponentWithJaxRsApiTemplate.class
)
Spec baseModel() {
return component("testComp",
dataElement("City"));
}
The template class needs to implement ModelSpecTemplate
. It has a single baseModel()
method, which takes a
model spec and returns a valid spec for a Program (e.g. Application).
public class ComponentWithJaxRsApiTemplate implements ModelSpecTemplate<ComponentSpec> {
@Override
public ApplicationSpec baseModel(ComponentSpec component) {
return application("myTestApp",
set("version", "1.2.3"),
option("experimental.control.cruds.jaxrs"),
component);
}
}
Composed annotations
The @TestModel
, @TestModelAssembly
and @TestProfile
annotations can be composed in a custom annotation to allow
for reuse. This can help make some test model definitions more succinct.
@TestModel()
@TestModelAssembly(
programType = "elements::JeeApplication",
template = ComponentWithJaxRsApiTemplate.class
)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ComponentWithJaxRsApi {
}
@ComponentWithJaxRsApi
Spec baseModel() {
return component("testComp",
dataElement("City"));
}
By default, if the test model assembly is enabled, the assembly mechanism will attempt to discover modules in the model spec by looking for specs of ElementTypes mentioned in any of the ModuleTypes.
This behaviour can be disabled by setting autoDiscoverModules
to false in the @TestModelAssembly
annotation.
The mechanism will then fall back to the behaviour provided by the ElementSpecFactory of the ElementSpec class of the
original spec.