Class WithSampleDataLoaded

java.lang.Object
fr.ght1pc9kc.testy.jooq.WithSampleDataLoaded
All Implemented Interfaces:
org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver

public final class WithSampleDataLoaded extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.ParameterResolver
This extension allows you to load test data into a previously created database.

These data are imported as a list of jOOQ records. The records are inserted in the same order they have been added to the extension in order to consider the external key constraints.

It is possible to insert data in an empty database thanks to the createTablesIfNotExists option.

The concerned tables are emptied and reloaded for each test to keep the consistency, however, for performance reasons, if the data has not been modified during a test, a tracker allows to signal to the extension that it is not necessary to refresh the data. This can improve test performance significantly.


 // Declare InMemory database
 private static final WithInMemoryDatasource wDs = WithInMemoryDatasource.builder().build();

 // Declare jOOQ DslContext
 private static final WithDslContext wDslContext = WithDslContext.builder()
         .setDatasourceExtension(wDs).build();

 // Declare records sample for each used tables
 private static final WithSampleDataLoaded wSamples = WithSampleDataLoaded.builder(wDslContext)
         .createTablesIfNotExists()               // Create table if not exist in database
         .addDataset(UsersRecordSamples.SAMPLE)
         .addDataset(UsersRolesSamples.SAMPLE)
         .build();

 // Use ChainedExtension to chain each declared extensions
 @RegisterExtension
 static ChainedExtension chain = ChainedExtension.outer(wDs)
         .append(wDslContext)
         .append(wSamples)
         .register();
 

You can now write your test


 @Test
 void should_get_raw_news(WithSampleDataLoaded.Tracker tracker) { // Inject the modification tracker
     tracker.skipNextSampleLoad();    // ask for skipping the next sample reload

     // ...
 }
 
  • Method Details

    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
    • builder

      public static WithSampleDataLoaded.SampleLoaderBuilder builder(org.junit.jupiter.api.extension.Extension ex)