Annotation Type MatsTest


  • @Retention(RUNTIME)
    @Target(TYPE)
    @ExtendWith({Extension_MatsRegistration.class,ParameterResolver_MatsEntities.class,FieldProcess_MatsTestAnnotations.class})
    public @interface MatsTest
    Convenience composite annotation for providing test infrastructure for Mats3 testing - notably what you get from the Jupiter extension Extension_Mats - as well as support for instantiating and registering endpoints as provided by the extensions Extension_MatsEndpoint and Extension_MatsAnnotatedClass by use of the two sub-annotations MatsTestEndpoint and MatsTestAnnotatedClass, and with the MatsTest annotation parameter matsAnnotatedClasses().

    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_Mats extension, 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 by AbstractMatsTest.getMatsFactory().
    • MatsFuturizer - as gotten by AbstractMatsTest.getMatsFuturizer().
    • MatsInitiator - as gotten by AbstractMatsTest.getMatsInitiator().
    • MatsTestLatch - as gotten by AbstractMatsTest.getMatsTestLatch().
    • MatsTestBrokerInterface - as gotten by AbstractMatsTest.getMatsTestBrokerInterface().
    • DataSource - as gotten by AbstractMatsTest.getDataSource(), which is an H2 DataSource created by invoking TestH2DataSource.createStandard(). This is only available if the Extension_Mats was created with a database (db() = true).

    The MatsTest annotation has parameters directing Extension_Mats to use a MatsSerializer other than the default, and to decide if it should be set up with an H2 database or not (as when using the Extension_Mats.createWithDb()).

    Extension_MatsEndpoint support

    The annotation MatsTestEndpoint can be applied to a field of type Extension_MatsEndpoint to 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 and BeforeEach methods.

    Extension_MatsAnnotatedClass support

    Support in two ways:
    1. MatsTestAnnotatedClass annotation on test classes' fields: The annotation MatsTestAnnotatedClass can be applied to fields in the test class whose type refers to a Mats3 SpringConfig annotations defined endpoint (classes annotated with MatsMapping and MatsClassMapping). Internally it uses the Extension_MatsAnnotatedClass feature. If the field is null, the class will be instantiated and registered by the extension, as if you called Extension_MatsAnnotatedClass.withAnnotatedMatsClasses(Class[]). However, if the field is already instantiated, the instance will be registered, as if you called Extension_MatsAnnotatedClass.withAnnotatedMatsInstances(Object[]). This happens after the constructor and field initialization, but before any test and BeforeEach methods.
    2. 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 the MatsTest annotation, so that it can create instances of the annotated classes before MatsTest inspects them.
    • Element Detail

      • db

        boolean db
        Should we create the Extension_Mats by invoking the Extension_Mats.createWithDb() method, which again creates a H2 DataSource by invoking TestH2DataSource.createStandard(). Default is false, no database.
        Returns:
        if the Extension_Mats should be created with a database.
        Default:
        false
      • serializerFactory

        java.lang.Class<? extends MatsTest.SerializerFactory> serializerFactory
        The serializer factory to use for the MatsFactory created by the extension. By default, the MatsSerializerJson is used.
        Returns:
        the serializer factory to use for the MatsFactory created by the extension.
        Default:
        io.mats3.test.jupiter.MatsTest.SerializerFactoryJson.class
      • matsAnnotatedClasses

        java.lang.Class<?>[] matsAnnotatedClasses
        Add classes here that you wish to be registered as Mats annotated classes.
        Default:
        {}