Skip to content

Commit

Permalink
Merge branch 'master' into db2-newer-image
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Jul 15, 2019
2 parents 0be7ef0 + 8f1421a commit e969b61
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/settings.gradle
Expand Up @@ -12,4 +12,5 @@ include 'selenium-container'
include 'singleton-container'
include 'spring-boot'
include 'cucumber'
include 'spock'
include 'spring-boot-kotlin-redis'
include 'spock'
28 changes: 28 additions & 0 deletions examples/spring-boot-kotlin-redis/build.gradle
@@ -0,0 +1,28 @@
plugins {
id("org.springframework.boot") version "2.1.5.RELEASE"
id("org.jetbrains.kotlin.jvm") version "1.3.31"
id("org.jetbrains.kotlin.plugin.spring") version "1.3.31"
}

apply plugin: 'io.spring.dependency-management'

repositories {
mavenCentral()
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.boot:spring-boot-starter-data-redis")
implementation("org.springframework.boot:spring-boot-starter-web")

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.testcontainers:testcontainers")
}

compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = ["-Xjsr305=strict"]
}
}
@@ -0,0 +1,27 @@
package com.example.redis

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController

@RestController
class ExampleController {

private val key = "testcontainers"

@Autowired
private lateinit var redisTemplate: RedisTemplate<String, String>

@PostMapping("/set-foo")
fun setFoo(@RequestBody value: String) {
redisTemplate.opsForValue().set(key, value)
}

@GetMapping("/get-foo")
fun getFoo(): String? {
return redisTemplate.opsForValue().get(key)
}
}
@@ -0,0 +1,11 @@
package com.example.redis

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class RedisApplication

fun main(args: Array<String>) {
runApplication<RedisApplication>(*args)
}
@@ -0,0 +1,37 @@
package com.example.redis

import org.junit.runner.RunWith
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.util.TestPropertyValues
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.ConfigurableApplicationContext
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringRunner
import org.testcontainers.containers.GenericContainer

@RunWith(SpringRunner::class)
@SpringBootTest
@ContextConfiguration(initializers = [AbstractIntegrationTest.Initializer::class])
@AutoConfigureMockMvc
abstract class AbstractIntegrationTest {

companion object {
val redisContainer = object : GenericContainer<Nothing>("redis:3-alpine") {
init {
withExposedPorts(6379)
}
}
}

internal class Initializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
override fun initialize(configurableApplicationContext: ConfigurableApplicationContext) {
redisContainer.start()

TestPropertyValues.of(
"spring.redis.host=${redisContainer.containerIpAddress}",
"spring.redis.port=${redisContainer.firstMappedPort}"
).applyTo(configurableApplicationContext.environment)
}
}
}
@@ -0,0 +1,11 @@
package com.example.redis

import org.junit.Test

class RedisApplicationTests : AbstractIntegrationTest() {

@Test
fun contextLoads() {
}

}
@@ -0,0 +1,29 @@
package com.example.redis

import org.hamcrest.CoreMatchers.containsString
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.test.web.servlet.setup.MockMvcBuilders


class RedisTest : AbstractIntegrationTest() {

@Autowired
private lateinit var mockMvc: MockMvc

@Test
fun testRedisFunctionality() {
val greeting = "Hello Testcontainers with Kotlin"
mockMvc.perform(post("/set-foo").content(greeting))
.andExpect(status().isOk)

mockMvc.perform(get("/get-foo"))
.andExpect(status().isOk)
.andExpect(content().string(containsString(greeting)))
}
}

0 comments on commit e969b61

Please sign in to comment.