title: Kotlintest; how to use abstract testcontainers class
body: <p>We have a Spring Boot project written in kotlin (for the first time) where we want to try kotlintest for writing tests.</p>
  
  <p>Our datasource is a postgresql database, and in our tests we utilize <code>org.testcontainers:postgresql</code> in order to not use a separate datasource like h2, or having to use a test schema in our actual database. Since firing up a testcontainer is a somewhat lengthy operation, we want it to only be done once across multiple tests.</p>
  
  <p>We use liquibase to evolve the database schema.</p>
  
  <p>So with that in mind we created an abstract class <code>ContainerDatabaseTest</code> which other tests can extend from:</p>
  
  <pre><code>class KPostgresContainer(imageName: String) : PostgreSQLContainer&lt;KPostgresContainer&gt;(imageName)
  
  @ContextConfiguration(initializers = [ContainerDatabaseTest.Initializer::class])
  abstract class ContainerDatabaseTest {
      companion object {
          val container: KPostgresContainer = KPostgresContainer("postgres:9.6.15")
                  .withDatabaseName("ic_orders_testcontainer_test")
                  .withUsername("ic_orders")
                  .withPassword("ic_orders")
  
          init {
              container.start()
          }
      }
  
      class Initializer : ApplicationContextInitializer&lt;ConfigurableApplicationContext&gt; {
          override fun initialize(configurableApplicationContext: ConfigurableApplicationContext) {
              val values = TestPropertyValues.of(
                      "spring.datasource.url=${container.jdbcUrl}",
                      "spring.datasource.username=${container.username}",
                      "spring.datasource.password=${container.password}",
                      "spring.liquibase.user=${container.username}",
                      "spring.liquibase.password=${container.password}"
              )
              values.applyTo(configurableApplicationContext)
          }
      }
  }
  </code></pre>
  
  <p>This fires up a testcontainer, once across all the tests that extend it during the test run.</p>
  
  <p>This is then used e.g. like this using JUnit5</p>
  
  <pre><code>@DataJpaTest
  @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
  class RepositoriesTests @Autowired constructor(
          val em: TestEntityManager,
          val moveOrderPlacementRepository: MoveOrderPlacementRepository
  ) : ContainerDatabaseTest() {
  
      @Test
      fun `Persist an object to database`() {
          em.persist(someObject)
  </code></pre>
  
  <p>But how can I rewrite these tests to <code>kotlintest</code>? In kotlintest you extend from a Spec (<code>abstract class</code> as well), and in kotlin you cannot extend from multiple classes as far as I understand.</p>
  
  <p>So how would I go about porting this test to <code>kotlintest</code>? I thought about <code>beforeAll</code> or <code>afterAll</code> in <code>ProjectConfig</code>, but I don't want or need a testcontainer for all tests, only those that requires a datasource. Then there is the <code>TestListener</code> concept described in the <a href="https://github.com/kotlintest/kotlintest/blob/master/doc/reference.md" rel="nofollow noreferrer">reference</a>, but I struggle to rewrite to that as well.</p>
  
  <p>Any pointers? Thanks!</p>