Created
September 2, 2019 22:00
-
-
Save razvn/a9e58f80c651369a8e5e1b1ba73d18c1 to your computer and use it in GitHub Desktop.
TestContainers SpringBoot Kotlin
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
spring.datasource.url=${DB_URL} | |
spring.datasource.username=${DB_USERNAME} | |
spring.datasource.password=${DB_PASSWORD} | |
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | |
spring.jpa.hibernate.ddl-auto=create-drop |
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 { | |
... | |
testImplementation("org.testcontainers:mysql:1.12.0") | |
... | |
} | |
... |
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
package net.razvan.webfluxjpatestcontiainers | |
import org.testcontainers.containers.MySQLContainer | |
object TestMySqlContainer : MySQLContainer<TestMySqlContainer>("mysql:5.7") { | |
override fun start() { | |
super.start() | |
System.setProperty("DB_URL", this.jdbcUrl) | |
System.setProperty("DB_USERNAME", this.username) | |
System.setProperty("DB_PASSWORD", this.password) | |
} | |
override fun stop() { | |
//do nothing jvm handles it | |
} | |
override fun getDriverClassName(): String = "com.mysql.cj.jdbc.Driver" | |
} |
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
package net.razvan.webfluxjpatestcontiainers | |
import org.assertj.core.api.Assertions.assertThat | |
import org.junit.jupiter.api.AfterAll | |
import org.junit.jupiter.api.BeforeAll | |
import org.junit.jupiter.api.Test | |
import org.junit.jupiter.api.TestInstance | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest | |
import org.springframework.data.repository.findByIdOrNull | |
@DataJpaTest | |
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | |
class UserRepositoryTest(@Autowired val userRepository: UserRepository) { | |
@BeforeAll | |
fun startDb() { | |
println("ClassRule Before") | |
TestMySqlContainer.start() | |
} | |
@AfterAll | |
fun stopDb() { | |
println("ClassRule After") | |
} | |
@Test | |
fun `save data works`() { | |
val user1 = userRepository.save(User(0, "TestUserSave1", 10)) | |
val user2 = userRepository.save(User(0, "TestUserSave2", 101)) | |
assertThat(user1) | |
.matches { it.id > 0 && it.name == "TestUserSave1" && it.age == 10 } | |
assertThat(user2) | |
.matches { it.id > 0 && it.name == "TestUserSave2" && it.age == 101 } | |
assertThat(userRepository.findAll()).containsExactly(user1, user2) | |
} | |
@Test | |
fun `findById works`() { | |
val user1 = userRepository.save(User(0, "TestUserFindById", 11)) | |
assertThat(user1) | |
.matches { it.id > 0 } | |
val dbUser = userRepository.findByIdOrNull(user1.id) | |
assertThat(dbUser) | |
.isNotNull | |
.matches { it!!.id == user1.id && it.name == "TestUserFindById" && it.age == 11 } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment