|
@@ -1,23 +1,36 @@
|
1
|
1
|
package com.example.bakerylab.controllers;
|
2
|
2
|
|
|
3
|
+import com.example.bakerylab.models.Baker;
|
3
|
4
|
import com.example.bakerylab.repositories.BakerRepository;
|
4
|
5
|
import org.junit.Before;
|
|
6
|
+import org.junit.Test;
|
5
|
7
|
import org.junit.runner.RunWith;
|
6
|
8
|
import org.mockito.InjectMocks;
|
7
|
9
|
import org.mockito.MockitoAnnotations;
|
8
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
11
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
10
|
12
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
13
|
+import org.springframework.http.MediaType;
|
11
|
14
|
import org.springframework.test.context.junit4.SpringRunner;
|
12
|
15
|
import org.springframework.test.web.servlet.MockMvc;
|
|
16
|
+import org.springframework.test.web.servlet.MvcResult;
|
13
|
17
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
14
|
18
|
|
15
|
|
-import static org.junit.Assert.*;
|
|
19
|
+import java.util.Optional;
|
|
20
|
+
|
|
21
|
+import static com.example.bakerylab.JsonUtil.asJsonString;
|
|
22
|
+import static org.mockito.BDDMockito.given;
|
|
23
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
24
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
|
25
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
16
|
26
|
|
17
|
27
|
|
18
|
28
|
@RunWith(SpringRunner.class)
|
19
|
29
|
@WebMvcTest(BakerController.class)
|
20
|
30
|
public class BakerControllerTest {
|
|
31
|
+ // === Instance Variables === //
|
|
32
|
+ private Baker froilan;
|
|
33
|
+ private final String GOOD_URL = "/bakers";
|
21
|
34
|
|
22
|
35
|
@Autowired
|
23
|
36
|
MockMvc mvc;
|
|
@@ -35,7 +48,63 @@ public class BakerControllerTest {
|
35
|
48
|
mvc = MockMvcBuilders
|
36
|
49
|
.standaloneSetup(bakerController)
|
37
|
50
|
.build();
|
|
51
|
+
|
|
52
|
+ froilan = new Baker("Froilan", "Master Baker", "Croissants");
|
|
53
|
+
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ // ================= Create Baker ================= //
|
|
57
|
+
|
|
58
|
+ @Test
|
|
59
|
+ public void testCreateBakerSuccess() throws Exception {
|
|
60
|
+
|
|
61
|
+ given(bakerRepository.save(froilan)).willReturn(froilan);
|
|
62
|
+
|
|
63
|
+ mvc.perform(post("/bakers")
|
|
64
|
+ .content(asJsonString(froilan))
|
|
65
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
66
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
67
|
+ .andExpect(status().isCreated());
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ @Test
|
|
71
|
+ public void testCreateBakerFail() throws Exception {
|
|
72
|
+ Baker b = new Baker ("Seth", "froilans prepper", "muffins");
|
|
73
|
+ String badUrl = "/bakers500";
|
|
74
|
+ given(bakerRepository.save(b)).willReturn(b);
|
|
75
|
+
|
|
76
|
+ mvc.perform(post(badUrl)
|
|
77
|
+ .content(asJsonString(froilan))
|
|
78
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
79
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
80
|
+ .andExpect(status().isNotFound());
|
|
81
|
+
|
38
|
82
|
}
|
39
|
83
|
|
|
84
|
+ // ================= Get Baker ================= //
|
|
85
|
+
|
|
86
|
+ @Test
|
|
87
|
+ public void getBakerById() throws Exception {
|
|
88
|
+ Long bakerId = 1L;
|
|
89
|
+
|
|
90
|
+ given(bakerRepository.findById(bakerId)).willReturn(Optional.of(froilan));
|
|
91
|
+
|
|
92
|
+ MvcResult response = mvc.perform(get(GOOD_URL)
|
|
93
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
94
|
+ .andExpect(status().isOk())
|
|
95
|
+ .andReturn();
|
|
96
|
+
|
|
97
|
+ System.out.println(response.getResponse().getContentAsString());
|
|
98
|
+// response.getResponse().getContentAsString();
|
|
99
|
+
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+ // ================= Get Bakers ================= //
|
|
104
|
+
|
|
105
|
+ // ================= Update Baker ================= //
|
|
106
|
+
|
|
107
|
+ // ================= Delete Baker ================= //
|
|
108
|
+
|
40
|
109
|
|
41
|
110
|
}
|