-
Notifications
You must be signed in to change notification settings - Fork 160
/
PetControllerTest.kt
114 lines (98 loc) · 4.12 KB
/
PetControllerTest.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package org.springframework.samples.petclinic.owner
import org.assertj.core.util.Lists
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.BDDMockito.given
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.FilterType
import org.springframework.test.context.junit.jupiter.SpringExtension
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.*
/**
* Test class for the [PetController]
*
* @author Colin But
*/
const val TEST_OWNER_ID = 1
const val TEST_PET_ID = 1
@ExtendWith(SpringExtension::class)
@WebMvcTest(value = [(PetController::class)], includeFilters = arrayOf(ComponentScan.Filter(value = [(PetTypeFormatter::class)], type = FilterType.ASSIGNABLE_TYPE)))
class PetControllerTest {
@Autowired
lateinit private var mockMvc: MockMvc
@MockBean
private lateinit var pets: PetRepository
@MockBean
private lateinit var owners: OwnerRepository
@BeforeEach
fun setup() {
val cat = PetType()
cat.id = 3
cat.name = "hamster"
given(this.pets.findPetTypes()).willReturn(Lists.newArrayList(cat))
given(this.owners.findById(TEST_OWNER_ID)).willReturn(Owner())
given(this.pets.findById(TEST_PET_ID)).willReturn(Pet())
}
@Test
fun testInitCreationForm() {
mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID))
.andExpect(status().isOk)
.andExpect(view().name("pets/createOrUpdatePetForm"))
.andExpect(model().attributeExists("owner"))
}
@Test
fun testProcessCreationFormSuccess() {
mockMvc.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID)
.param("name", "Betty")
.param("type", "hamster")
.param("birthDate", "2015-02-12")
)
.andExpect(status().is3xxRedirection)
.andExpect(view().name("redirect:/owners/{ownerId}"))
}
@Test
fun testProcessCreationFormHasErrors() {
mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
.param("name", "Betty")
.param("birthDate", "2015-02-12")
)
.andExpect(model().attributeHasNoErrors("owner"))
.andExpect(model().attributeHasErrors("pet"))
.andExpect(status().isOk)
.andExpect(view().name("pets/createOrUpdatePetForm"))
}
@Test
fun testInitUpdateForm() {
mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID))
.andExpect(status().isOk)
.andExpect(model().attributeExists("owner"))
.andExpect(view().name("pets/createOrUpdatePetForm"))
}
@Test
fun testProcessUpdateFormSuccess() {
mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
.param("name", "Betty")
.param("type", "hamster")
.param("birthDate", "2015-02-12")
)
.andExpect(status().is3xxRedirection)
.andExpect(view().name("redirect:/owners/{ownerId}"))
}
@Test
fun testProcessUpdateFormHasErrors() {
mockMvc.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID)
.param("name", "Betty")
.param("birthDate", "2015-02-12")
)
.andExpect(model().attributeHasNoErrors("owner"))
.andExpect(model().attributeHasErrors("pet"))
.andExpect(status().isOk)
.andExpect(view().name("pets/createOrUpdatePetForm"))
}
}