테스트 통합 환경 설정 추가
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package kr.co.vividnext.sodalive.support
|
||||
|
||||
import org.springframework.context.ApplicationContextInitializer
|
||||
import org.springframework.context.ConfigurableApplicationContext
|
||||
import redis.embedded.RedisServer
|
||||
|
||||
class EmbeddedRedisInitializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
override fun initialize(applicationContext: ConfigurableApplicationContext) {
|
||||
EmbeddedRedisHolder.start()
|
||||
}
|
||||
}
|
||||
|
||||
private object EmbeddedRedisHolder {
|
||||
private const val PORT = 16379
|
||||
private var redisServer: RedisServer? = null
|
||||
private var shutdownHookRegistered = false
|
||||
|
||||
@Synchronized
|
||||
fun start() {
|
||||
if (redisServer != null) {
|
||||
return
|
||||
}
|
||||
|
||||
redisServer = RedisServer.newRedisServer()
|
||||
.port(PORT)
|
||||
.setting("bind 127.0.0.1")
|
||||
.setting("daemonize no")
|
||||
.setting("appendonly no")
|
||||
.build()
|
||||
.also { it.start() }
|
||||
|
||||
if (!shutdownHookRegistered) {
|
||||
Runtime.getRuntime().addShutdownHook(
|
||||
Thread {
|
||||
stop()
|
||||
}
|
||||
)
|
||||
shutdownHookRegistered = true
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun stop() {
|
||||
redisServer?.stop()
|
||||
redisServer = null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package kr.co.vividnext.sodalive.support
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
import org.springframework.data.redis.core.StringRedisTemplate
|
||||
import javax.persistence.EntityManager
|
||||
|
||||
@SpringBootTest
|
||||
class SpringBootIntegrationSampleTest {
|
||||
@Autowired
|
||||
private lateinit var entityManager: EntityManager
|
||||
|
||||
@Autowired
|
||||
private lateinit var stringRedisTemplate: StringRedisTemplate
|
||||
|
||||
@Test
|
||||
fun shouldUseH2AndRedisInSpringBootIntegrationTest() {
|
||||
val databaseResult = entityManager
|
||||
.createNativeQuery("select 1")
|
||||
.singleResult
|
||||
|
||||
val redisKey = "test:integration-sample"
|
||||
stringRedisTemplate.opsForValue().set(redisKey, "ok")
|
||||
val redisResult = stringRedisTemplate.opsForValue().get(redisKey)
|
||||
|
||||
assertEquals(1, (databaseResult as Number).toInt())
|
||||
assertEquals("ok", redisResult)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user