Class EmbeddedDatabaseExtension

  • All Implemented Interfaces:
    org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, 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 class EmbeddedDatabaseExtension
    extends Object
    implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.ParameterResolver
    A JUnit Jupiter extension that can be used to start/stop an embedded database.

    Note that this extension can be used with:
    • The ExtendWith annotation, in this case the embedded server will be started before all tests and stopped after all tests.
    • Using the RegisterExtension annotation, in this case the embedded server will be
      • Started before all tests and stopped after all tests if the extension is declared as static or the test class is used TestInstance.Lifecycle.PER_CLASS mode.
      • Started before each test and stopped after each test if the extension is not declared as static and the test class is used with TestInstance.Lifecycle.PER_METHOD mode (the default).
    This extension will also allow injection of EmbeddedDatabase into test methods.

    Here is an example using the RegisterExtension annotation:
    
       class MyDaoTest {
         @RegisterExtension
         static EmbeddedDatabaseExtension extension = new EmbeddedDatabaseExtension(
           new EmbeddedDatabaseBuilder()
             .generateUniqueName(true)
             .addScript("classpath:/sql/init.sql")
             .addScript("classpath:/sql/data.sql")
             .build()
         );
    
         @Test
         void test1(EmbeddedDatabase db) throws Exception {
           Assertions.assertEquals(count(db.getConnection()), 2);
         }
       }
     
    Note that this extension can also be used with the EmbeddedDatabaseConfiguration annotation. Here is the exact same example as below:
    
       @ExtendWith(EmbeddedDatabaseExtension.class)
       @EmbeddedDatabaseConfiguration(
         generateUniqueName = true,
         scripts = {"classpath:/sql/init.sql", "classpath:/sql/data.sql"}
       )
       class MyDaoTest {
         @Test
         void test1(EmbeddedDatabase db) throws Exception {
           Assertions.assertEquals(count(db.getConnection()), 2);
         }
       }
     
    See Also:
    EmbeddedDatabaseConfiguration, EmbeddedDatabase, EmbeddedDatabaseBuilder
    • Constructor Summary

      Constructors 
      Constructor Description
      EmbeddedDatabaseExtension()
      Create the extension, a default EmbeddedDatabase will be used.
      EmbeddedDatabaseExtension​(org.springframework.jdbc.datasource.embedded.EmbeddedDatabase db)
      Create the extension, the given EmbeddedDatabase will be used.
    • Constructor Detail

      • EmbeddedDatabaseExtension

        public EmbeddedDatabaseExtension()
        Create the extension, a default EmbeddedDatabase will be used.
      • EmbeddedDatabaseExtension

        public EmbeddedDatabaseExtension​(org.springframework.jdbc.datasource.embedded.EmbeddedDatabase db)
        Create the extension, the given EmbeddedDatabase will be used.
        Parameters:
        db - The given embedded database.
    • Method Detail

      • beforeAll

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

        public void afterAll​(org.junit.jupiter.api.extension.ExtensionContext context)
        Specified by:
        afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
      • beforeEach

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

        public void afterEach​(org.junit.jupiter.api.extension.ExtensionContext context)
        Specified by:
        afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      • 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