Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin example #1521

Merged
merged 6 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/settings.gradle
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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"]
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.redis

import org.junit.Test

class RedisApplicationTests : AbstractIntegrationTest() {

@Test
fun contextLoads() {
}

}
Original file line number Diff line number Diff line change
@@ -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)))
}
}