Class Rule_Mats

All Implemented Interfaces:
org.junit.rules.TestRule

public class Rule_Mats extends AbstractMatsTest<String> implements org.junit.rules.TestRule
ClassRule which provides a full Mats harness for unit testing by creating MatsFactory utilizing an in-vm Active MQ broker, and optionally a TestH2DataSource for database tests.

Notice: If you are in a Spring-context, this is probably not what you are looking for, as the MatsFactory then should reside as a bean in the Spring context. Look in the 'mats-spring-test' package for testing tools for Spring.

By default the rule will create a MatsSerializerJson which will be the serializer utilized by the created MatsFactory. Should one want to use a different serializer which serializes to the type of String then this can be specified using the method create(MatsSerializer). However should one want to specify a serializer which serializes into anything other than String, then Rule_MatsGeneric offers this possibility.

Rule_Mats shall be considered a ClassRule and thus annotated as such, being a ClassRule also means that the instance field shall be static. Therefore to utilize Rule_Mats one should add it to a test class in this fashion:

     public class YourTestClass {
         @ClassRule
         public static final Rule_Mats MATS = Rule_Mats.create() // Or provide your own serializer
     }
 
To get a variant that has a TestH2DataSource contained, and the MatsFactory set up with transactional handling of that, use the createWithDb() methods. In this case, you might want to clean the database before each test method, which can be accomplished as such:
     public class YourTestClass {
         @ClassRule
         public static Rule_Mats MATS = Rule_Mats.createWithDb() // Or provide your own serializer

         @Before  // Will clean the database before each test - if this is what you want.
         public void cleanDatabase() {
             MATS.getDataSource().cleanDatabase()
         }
     }
 
See Also: