-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimalControllerTest.java
116 lines (91 loc) · 3.74 KB
/
AnimalControllerTest.java
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
115
116
package demo;
import org.junit.Test;
import org.junit.runner.RunWith;
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.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Collections;
import java.util.Optional;
import static org.hamcrest.Matchers.is;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @author Created by tom on 13.05.2017.
*/
@WebMvcTest(controllers = {AnimalController.class})
@RunWith(SpringRunner.class)
public class AnimalControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private AnimalService animalService;
@Test
public void getAnimals() throws Exception {
given(animalService.getAllAnimals())
.willReturn(Collections.singletonList(new Dog("molly")));
mvc.perform(get("/animals"))
.andDo(print())
.andExpect(status().isOk())
// .andExpect(content().json("[{\"name\":\"molly\"}]"));
.andExpect(jsonPath("$[0].name", is("molly")));
}
@Test
public void getAnimalByName_notExisting() throws Exception {
given(animalService.getAnimalByName("molly"))
.willReturn(Optional.empty());
mvc.perform(get("/animals/molly"))
.andDo(print())
.andExpect(status().isNotFound());
}
@Test
public void getAnimalByName_existing() throws Exception {
given(animalService.getAnimalByName("molly"))
.willReturn(Optional.of(new Dog("molly")));
mvc.perform(get("/animals/molly"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is("molly")));
}
@Test
public void deleteAnimalByName_notExisting() throws Exception {
given(animalService.getAnimalByName("molly"))
.willReturn(Optional.empty());
mvc.perform(delete("/animals/molly"))
.andDo(print())
.andExpect(status().isNotFound());
}
@Test
public void deleteAnimalByName_cannotRemove() throws Exception {
final Dog molly = new Dog("molly");
given(animalService.getAnimalByName("molly"))
.willReturn(Optional.of(molly));
given(animalService.removeAnimal(molly))
.willReturn(false);
mvc.perform(delete("/animals/molly"))
.andDo(print())
.andExpect(status().isBadRequest());
}
@Test
public void deleteAnimalByName_ok() throws Exception {
final Dog molly = new Dog("molly");
given(animalService.getAnimalByName("molly"))
.willReturn(Optional.of(molly));
given(animalService.removeAnimal(molly))
.willReturn(true);
mvc.perform(delete("/animals/molly"))
.andDo(print())
.andExpect(status().isOk());
}
@Test
public void upsertAnimalByName() throws Exception {
mvc.perform(put("/animals/molly")).andDo(print()).andExpect(status()
.isNotImplemented());
}
}