BAEL-2910 MockMvc Kotlin DSL
* configured spring repo in the 'parent-kotlin' model to allow using spring's milestone artifacts * configured jackson kotlin module in the 'parent-kotlin' to allow easy use of kotlin data classes with jackson * switched spring-mvc-kotlin from explicit spring dependencies to spring-boot * mockmvc dsl article's source and tests
This commit is contained in:
parent
9d7d7b4dde
commit
212326a754
@ -26,6 +26,11 @@
|
|||||||
<id>kotlin-eap</id>
|
<id>kotlin-eap</id>
|
||||||
<url>http://dl.bintray.com/kotlin/kotlin-eap</url>
|
<url>http://dl.bintray.com/kotlin/kotlin-eap</url>
|
||||||
</repository>
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>spring-milestone</id>
|
||||||
|
<name>Spring Milestone Repository</name>
|
||||||
|
<url>http://repo.spring.io/milestone</url>
|
||||||
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
<pluginRepositories>
|
<pluginRepositories>
|
||||||
@ -40,7 +45,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-dependencies</artifactId>
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
<version>2.0.1.RELEASE</version>
|
<version>2.2.0.M4</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
@ -80,6 +85,10 @@
|
|||||||
<artifactId>ktor-gson</artifactId>
|
<artifactId>ktor-gson</artifactId>
|
||||||
<version>${ktor.io.version}</version>
|
<version>${ktor.io.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.module</groupId>
|
||||||
|
<artifactId>jackson-module-kotlin</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jetbrains.kotlin</groupId>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
@ -15,12 +15,12 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-webmvc</artifactId>
|
<artifactId>spring-boot-starter-json</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.thymeleaf</groupId>
|
<groupId>org.thymeleaf</groupId>
|
||||||
@ -46,8 +46,8 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.kotlin.mockmvc
|
||||||
|
|
||||||
|
import org.springframework.http.MediaType
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod
|
||||||
|
import org.springframework.web.bind.annotation.RestController
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/mockmvc")
|
||||||
|
class MockMvcController {
|
||||||
|
|
||||||
|
@RequestMapping(value = ["/validate"], method = [RequestMethod.POST], produces = [MediaType.APPLICATION_JSON_VALUE])
|
||||||
|
fun validate(@RequestBody request: Request): Response {
|
||||||
|
val error = if (request.name.first == "admin") {
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
ERROR
|
||||||
|
}
|
||||||
|
return Response(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val ERROR = "invalid user"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.kotlin.mockmvc
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude
|
||||||
|
|
||||||
|
data class Name(val first: String, val last: String)
|
||||||
|
|
||||||
|
data class Request(val name: Name)
|
||||||
|
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
data class Response(val error: String?)
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.baeldung.kotlin.mockmvc
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||||
|
import org.springframework.http.MediaType
|
||||||
|
import org.springframework.http.ResponseEntity.status
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner
|
||||||
|
import org.springframework.test.web.servlet.MockMvc
|
||||||
|
import org.springframework.test.web.servlet.post
|
||||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
|
||||||
|
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
|
||||||
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
|
||||||
|
|
||||||
|
@RunWith(SpringRunner::class)
|
||||||
|
@WebMvcTest
|
||||||
|
class MockMvcControllerTest {
|
||||||
|
|
||||||
|
@Autowired lateinit var mockMvc: MockMvc
|
||||||
|
@Autowired lateinit var mapper: ObjectMapper
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when supported user is given then raw MockMvc-based validation is successful`() {
|
||||||
|
mockMvc.perform(MockMvcRequestBuilders
|
||||||
|
.post("/mockmvc/validate")
|
||||||
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(mapper.writeValueAsString(Request(Name("admin", "")))))
|
||||||
|
.andExpect(MockMvcResultMatchers.status().isOk)
|
||||||
|
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
|
||||||
|
.andExpect(MockMvcResultMatchers.content().string("{}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when supported user is given then kotlin DSL-based validation is successful`() {
|
||||||
|
doTest(Request(Name("admin", "")), Response(null))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when unsupported user is given then validation is failed`() {
|
||||||
|
doTest(Request(Name("some-name", "some-surname")), Response(MockMvcController.ERROR))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun doTest(input: Request, expectation: Response) {
|
||||||
|
mockMvc.post("/mockmvc/validate") {
|
||||||
|
contentType = MediaType.APPLICATION_JSON
|
||||||
|
content = mapper.writeValueAsString(input)
|
||||||
|
accept = MediaType.APPLICATION_JSON
|
||||||
|
}.andExpect {
|
||||||
|
status { isOk }
|
||||||
|
content { contentType(MediaType.APPLICATION_JSON) }
|
||||||
|
content { json(mapper.writeValueAsString(expectation)) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
class MockMvcApplication
|
Loading…
x
Reference in New Issue
Block a user