Last active
August 24, 2024 09:52
-
-
Save joonseolee/fd6ddd5bdbb4bbb51357ba5da885ec20 to your computer and use it in GitHub Desktop.
testcontainer kotlin + mysql example
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 { | |
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' | |
} |
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
@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) | |
} | |
} |
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
@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