Last active
July 18, 2023 12:54
-
-
Save prondzyn/ce8fc98687db97c57af7cef1eaad0871 to your computer and use it in GitHub Desktop.
An example usage of @JooqTest (Spring Boot, Kotlin, kotest, Testcontainers, jOOQ)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
dependencies { | |
... | |
// Possibly not everything is needed here. To verify. | |
val kotestVersion = "5.6.2" | |
testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion") | |
testImplementation("io.kotest:kotest-property:$kotestVersion") | |
testImplementation("io.kotest.extensions:kotest-extensions-spring:1.1.2") | |
testImplementation("com.ninja-squad:springmockk:3.1.2") | |
testImplementation("org.testcontainers:junit-jupiter") | |
testImplementation("org.testcontainers:postgresql") | |
testImplementation("io.kotest.extensions:kotest-extensions-testcontainers:1.3.4") | |
} | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.kotest.core.spec.style.WordSpec | |
import io.kotest.extensions.spring.SpringExtension | |
import io.kotest.matchers.* | |
import generated.tables.daos.FacilityDao | |
import generated.jooq.tables.pojos.Facility | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.boot.test.autoconfigure.jooq.JooqTest | |
import org.springframework.context.annotation.Import | |
import org.springframework.test.context.DynamicPropertyRegistry | |
import org.springframework.test.context.DynamicPropertySource | |
import org.testcontainers.containers.PostgreSQLContainer | |
import org.testcontainers.junit.jupiter.Container | |
import org.testcontainers.utility.DockerImageName | |
@JooqTest | |
abstract class JooqWordSpec : WordSpec() { | |
override fun extensions() = listOf(SpringExtension) | |
@Autowired | |
private lateinit var dsl: DSLContext | |
override suspend fun afterAny(testCase: TestCase, result: TestResult) { | |
super.afterAny(testCase, result) | |
dsl.meta() | |
.filterSchemas { it.name == "public" } | |
.filterTables { it.type.isTable } | |
.filterTables { it.name != FLYWAY_SCHEMA_HISTORY.name } | |
.tables | |
.forEach { dsl.truncate(it).cascade().execute() } | |
} | |
companion object { | |
@Container | |
@JvmField | |
var container = PostgreSQLContainer<Nothing>( | |
DockerImageName | |
.parse("postgis/postgis:14-3.3-alpine") | |
.asCompatibleSubstituteFor("postgres") | |
).apply { | |
withDatabaseName("testdb") | |
withUsername("db_user") | |
withPassword("db_pass") | |
} | |
init { | |
container.start() | |
} | |
@DynamicPropertySource | |
@JvmStatic | |
fun datasourceProperties(registry: DynamicPropertyRegistry) { | |
registry.add("spring.datasource.url", container::getJdbcUrl) | |
registry.add("spring.datasource.password", container::getPassword) | |
registry.add("spring.datasource.username", container::getUsername) | |
} | |
} | |
} | |
@Import(value = [FacilityRepository::class, FacilityDao::class, /* and everything else what is needed */]) | |
class FacilityRepositoryTest : JooqWordSpec() { | |
@Autowired | |
private lateinit var facilityRepository: FacilityRepository | |
init { | |
"facility repository" should { | |
"create and return facility" { | |
// given | |
val facility = Facility() | |
// when | |
val createdFacility = facilityRepository.create(facility) | |
// then | |
createdFacility.id shouldNotBe null | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment