BAEL-755 Kotlin and Spring Boot 2.x tutorial

This commit is contained in:
tschiman
2017-05-26 18:04:48 -06:00
parent 2bfc5bd979
commit 7bbfde57e1
8 changed files with 238 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
package com.baeldung.springbootkotlin
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class HelloController @Autowired constructor(val helloService: HelloService) {
@GetMapping("/hello")
fun helloKotlin(): String {
return "hello world"
}
@GetMapping("/hello-service")
fun helloKotlinService(): String {
return helloService.getHello()
}
@GetMapping("/hello-poko")
fun helloPoko(): HelloPoko {
return HelloPoko("Hello from the poko")
}
}

View File

@@ -0,0 +1,3 @@
package com.baeldung.springbootkotlin
data class HelloPoko constructor(val greeting: String)

View File

@@ -0,0 +1,10 @@
package com.baeldung.springbootkotlin
import org.springframework.stereotype.Service
@Service
class HelloService {
fun getHello(): String {
return "hello service"
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.springbootkotlin
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class KotlinDemoApplication
fun main(args: Array<String>) {
SpringApplication.run(KotlinDemoApplication::class.java, *args)
}

View File

@@ -0,0 +1,52 @@
package com.example.kotlindemo
import com.baeldung.springbootkotlin.HelloPoko
import com.baeldung.springbootkotlin.KotlinDemoApplication
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.http.HttpStatus
import org.springframework.test.context.junit4.SpringRunner
@RunWith(SpringRunner::class)
@SpringBootTest(classes = arrayOf(KotlinDemoApplication::class), webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class KotlinDemoApplicationTests {
@Autowired
val testRestTemplate: TestRestTemplate? = null
@Test
fun contextLoads() {
}
@Test
fun testHelloController() {
var result = testRestTemplate?.getForEntity("/hello", String::class.java)
Assert.assertNotNull(result)
Assert.assertEquals(result?.statusCode, HttpStatus.OK)
Assert.assertEquals(result?.body, "hello world")
}
@Test
fun testHelloService() {
var result = testRestTemplate?.getForEntity("/hello-service", String::class.java)
Assert.assertNotNull(result)
Assert.assertEquals(result?.statusCode, HttpStatus.OK)
Assert.assertEquals(result?.body, "hello service")
}
@Test
fun testHelloPoko() {
var result = testRestTemplate?.getForEntity("/hello-poko", HelloPoko::class.java)
Assert.assertNotNull(result)
Assert.assertEquals(result?.statusCode, HttpStatus.OK)
Assert.assertEquals(result?.body, HelloPoko("Hello from the poko"))
}
}