Annotation Type MatsTest
-
@Retention(RUNTIME) @Target(TYPE) @ExtendWith({Extension_MatsRegistration.class,ParameterResolver_MatsEntities.class,FieldProcess_MatsTestAnnotations.class}) public @interface MatsTestConvenience composite annotation for providing test infrastructure for Mats3 testing - notably what you get from the Jupiter extensionExtension_Mats- as well as support for instantiating and registering endpoints as provided by the extensionsExtension_MatsEndpointandExtension_MatsAnnotatedClassby use of the two sub-annotationsMatsTestEndpointandMatsTestAnnotatedClass, and with the MatsTest annotation parametermatsAnnotatedClasses().Note that this tool is not a requirement for writing tests for Mats3! This is a pure convenience that can remove a minor bit of boilerplate, and can make the tests a few lines smaller, and maybe - for developers already proficient with Mats3 - a bit quicker to read by hiding away some minor elements of infrastructure. You should probably NOT start your Mats3 testing journey with this tool - rather understand how Mats3 works, and how the testing tools works, before shaving off these few infrastructural code bytes.
By annotating a test class with this composite annotation, the tool instantiates and registers the
Extension_Matsextension, and then provides parameter resolving for test methods (and @Nested-tests construction injection), for the following parameter types:Extension_Mats- The extension itself.MatsFactory- as gotten byAbstractMatsTest.getMatsFactory().MatsFuturizer- as gotten byAbstractMatsTest.getMatsFuturizer().MatsInitiator- as gotten byAbstractMatsTest.getMatsInitiator().MatsTestLatch- as gotten byAbstractMatsTest.getMatsTestLatch().MatsTestBrokerInterface- as gotten byAbstractMatsTest.getMatsTestBrokerInterface().DataSource- as gotten byAbstractMatsTest.getDataSource(), which is an H2 DataSource created by invokingTestH2DataSource.createStandard(). This is only available if theExtension_Matswas created with a database (db()=true).
The MatsTest annotation has parameters directing
Extension_Matsto use aMatsSerializerother than the default, and to decide if it should be set up with an H2 database or not (as when using theExtension_Mats.createWithDb()).
The annotationExtension_MatsEndpointsupportMatsTestEndpointcan be applied to a field of typeExtension_MatsEndpointto inject a test endpoint into the test class. Thus, there is no need to initialize the field, as this extension will take care of resolving relevant types, and constructing the instance, and setting the field before the test executes. This happens after the constructor, but before any test andBeforeEachmethods.
Support in two ways:Extension_MatsAnnotatedClasssupportMatsTestAnnotatedClassannotation on test classes' fields: The annotationMatsTestAnnotatedClasscan be applied to fields in the test class whose type refers to a Mats3 SpringConfig annotations defined endpoint (classes annotated withMatsMappingandMatsClassMapping). Internally it uses theExtension_MatsAnnotatedClassfeature. If the field is null, the class will be instantiated and registered by the extension, as if you calledExtension_MatsAnnotatedClass.withAnnotatedMatsClasses(Class[]). However, if the field is already instantiated, the instance will be registered, as if you calledExtension_MatsAnnotatedClass.withAnnotatedMatsInstances(Object[]). This happens after the constructor and field initialization, but before any test andBeforeEachmethods.- MatsTest's
matsAnnotatedClasses()parameter: You may list classes that are annotated with Mats3 SpringConfig annotations, and these will be instantiated and registered before the test executes.
Mockito interaction
If the test class uses Mockito, and @InjectMocks is used, then this becomes sensitive to the order of the annotations: The@ExtendWith(MockitoExtension.class)annotation should be placed above theMatsTestannotation, so that it can create instances of the annotated classes before MatsTest inspects them.
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description booleandbShould we create theExtension_Matsby invoking theExtension_Mats.createWithDb()method, which again creates a H2 DataSource by invokingTestH2DataSource.createStandard().java.lang.Class<?>[]matsAnnotatedClassesAdd classes here that you wish to be registered as Mats annotated classes.java.lang.Class<? extends MatsTest.SerializerFactory>serializerFactoryThe serializer factory to use for theMatsFactorycreated by the extension.
-
-
-
Element Detail
-
db
boolean db
Should we create theExtension_Matsby invoking theExtension_Mats.createWithDb()method, which again creates a H2 DataSource by invokingTestH2DataSource.createStandard(). Default isfalse, no database.- Returns:
- if the
Extension_Matsshould be created with a database.
- Default:
- false
-
-
-
serializerFactory
java.lang.Class<? extends MatsTest.SerializerFactory> serializerFactory
The serializer factory to use for theMatsFactorycreated by the extension. By default, theMatsSerializerJsonis used.- Returns:
- the serializer factory to use for the
MatsFactorycreated by the extension.
- Default:
- io.mats3.test.jupiter.MatsTest.SerializerFactoryJson.class
-
-