-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeanValidationContainerConstraintsApplicationTests.kt
46 lines (40 loc) · 1.52 KB
/
BeanValidationContainerConstraintsApplicationTests.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.example.beanvalidationcontainerconstraints
import org.hamcrest.Matchers.hasSize
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
@RunWith(SpringRunner::class)
@SpringBootTest
@AutoConfigureMockMvc
class BeanValidationContainerConstraintsApplicationTests {
@Autowired lateinit var mockMvc: MockMvc
@Test
fun `should fail with two validation errors`() {
invokeTest("/samples")
}
@Test
fun `should fail with two validation errors when java request body`() {
invokeTest("/samples/java")
}
fun invokeTest(endpoint: String) {
mockMvc.perform(post(endpoint)
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"name": "",
"someMap": { "some": "" }
}""".trimIndent()))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isBadRequest)
.andExpect(jsonPath("errors", hasSize<String>(2)))
}
}