Skip to content

Instantly share code, notes, and snippets.

@joonseolee
Last active August 24, 2024 09:52
Show Gist options
  • Save joonseolee/fd6ddd5bdbb4bbb51357ba5da885ec20 to your computer and use it in GitHub Desktop.
Save joonseolee/fd6ddd5bdbb4bbb51357ba5da885ec20 to your computer and use it in GitHub Desktop.
testcontainer kotlin + mysql example
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
runtimeOnly 'com.mysql:mysql-connector-j'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'
testImplementation 'org.testcontainers:junit-jupiter'
testImplementation 'org.testcontainers:mysql'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
@Testcontainers
@Import(TestcontainersConfiguration::class)
class PersonControllerTest {
@Autowired
private lateinit var personRepository: PersonRepository
@Test
fun given_when_then() {
val persons = personRepository.findAll()
assertThat(persons).isEmpty()
personRepository.save(Person("joonseo", 20))
val newPersons = personRepository.findAll()
assertThat(newPersons).hasSize(1)
}
}
@TestConfiguration(proxyBeanMethods = false)
class TestcontainersConfiguration {
@Bean
@ServiceConnection
fun mysqlContainer(): MySQLContainer<*> {
return MySQLContainer(DockerImageName.parse("mysql:latest"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment