mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 00:32:34 +03:00
add tests for GreetingController
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id("org.springframework.boot") version "3.2.4"
|
id("org.springframework.boot") version "3.2.4"
|
||||||
id("io.spring.dependency-management") version "1.1.4"
|
id("io.spring.dependency-management") version "1.1.4"
|
||||||
|
jacoco
|
||||||
kotlin("jvm") version "2.0.20"
|
kotlin("jvm") version "2.0.20"
|
||||||
kotlin("plugin.jpa") version "1.9.23"
|
kotlin("plugin.jpa") version "1.9.23"
|
||||||
kotlin("plugin.serialization") version "2.0.20"
|
kotlin("plugin.serialization") version "2.0.20"
|
||||||
@@ -34,13 +33,22 @@ dependencies {
|
|||||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType<KotlinCompile> {
|
kotlin {
|
||||||
kotlinOptions {
|
compilerOptions {
|
||||||
freeCompilerArgs += "-Xjsr305=strict"
|
freeCompilerArgs.addAll("-Xjsr305=strict")
|
||||||
jvmTarget = "17"
|
apiVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_7)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType<Test> {
|
tasks.withType<Test> {
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tasks.test {
|
||||||
|
finalizedBy(tasks.jacocoTestReport) // report is always generated after tests run
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.jacocoTestReport {
|
||||||
|
dependsOn(tasks.test) // tests are required to run before generating the report
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.example.demo.controllers
|
|||||||
|
|
||||||
import com.example.demo.provider.html.renderProductTable
|
import com.example.demo.provider.html.renderProductTable
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody
|
||||||
import org.springframework.web.bind.annotation.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -11,7 +12,8 @@ class GreetingController {
|
|||||||
return "Hello World!"
|
return "Hello World!"
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/example/html")
|
@GetMapping(value = ["/example/html"], produces = ["text/html"])
|
||||||
|
@ResponseBody
|
||||||
fun exampleHtml(): String {
|
fun exampleHtml(): String {
|
||||||
return renderProductTable()
|
return renderProductTable()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.example.demo.controllers
|
||||||
|
|
||||||
|
import org.hamcrest.core.StringContains
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||||
|
import org.springframework.test.web.servlet.MockMvc
|
||||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
|
||||||
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
|
||||||
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
|
||||||
|
import kotlin.test.Test
|
||||||
|
|
||||||
|
@WebMvcTest(GreetingController::class)
|
||||||
|
class GreetingControllerTest(@Autowired val mockMvc: MockMvc) {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun greetings_shouldSeeGreetingMessage() {
|
||||||
|
mockMvc.perform(get("/greeting"))
|
||||||
|
.andExpect(status().isOk)
|
||||||
|
.andExpect(content().contentType("text/plain;charset=UTF-8"))
|
||||||
|
.andExpect(content().string("Hello World!"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun exampleHtml_shouldSeeRenderedHtml() {
|
||||||
|
mockMvc.perform(get("/example/html"))
|
||||||
|
.andExpect(status().isOk)
|
||||||
|
.andExpect(content().contentType("text/html;charset=UTF-8"))
|
||||||
|
.andExpect(content().string(StringContains("Product")))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user