|
1 | | -package com.baeldung.validator; |
2 | | - |
3 | | -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
4 | | -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
5 | | -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
6 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
7 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; |
8 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
9 | | -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; |
10 | | - |
11 | | -import org.junit.Before; |
12 | | -import org.junit.Test; |
13 | | -import org.junit.runner.RunWith; |
14 | | -import org.springframework.beans.factory.annotation.Autowired; |
15 | | -import org.springframework.boot.test.SpringApplicationConfiguration; |
16 | | -import org.springframework.http.MediaType; |
17 | | -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
18 | | -import org.springframework.test.context.web.WebAppConfiguration; |
19 | | -import org.springframework.test.web.servlet.MockMvc; |
20 | | -import org.springframework.test.web.servlet.ResultActions; |
21 | | -import org.springframework.test.web.servlet.ResultMatcher; |
22 | | -import org.springframework.web.context.WebApplicationContext; |
23 | | - |
24 | | -import com.baeldung.SpringDataRestApplication; |
25 | | -import com.baeldung.models.WebsiteUser; |
26 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
27 | | - |
28 | | -@RunWith(SpringJUnit4ClassRunner.class) |
29 | | -@SpringApplicationConfiguration(classes = SpringDataRestApplication.class) |
30 | | -@WebAppConfiguration |
31 | | -public class SpringDataRestValidatorTest { |
32 | | - public static final String URL = "http://localhost"; |
33 | | - |
34 | | - private MockMvc mockMvc; |
35 | | - |
36 | | - @Autowired |
37 | | - protected WebApplicationContext wac; |
38 | | - |
39 | | - @Before |
40 | | - public void setup() { |
41 | | - mockMvc = webAppContextSetup(wac).build(); |
42 | | - } |
43 | | - |
44 | | - @Test |
45 | | - public void whenStartingApplication_thenCorrectStatusCode() throws Exception { |
46 | | - mockMvc.perform(get("/users")).andExpect(status().is2xxSuccessful()); |
47 | | - }; |
48 | | - |
49 | | - @Test |
50 | | - public void whenAddingNewCorrectUser_thenCorrectStatusCodeAndResponse() throws Exception { |
51 | | - WebsiteUser user = new WebsiteUser(); |
52 | | - user.setEmail("john.doe@john.com"); |
53 | | - user.setName("John Doe"); |
54 | | - |
55 | | - mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) |
56 | | - .andExpect(status().is2xxSuccessful()) |
57 | | - .andExpect(redirectedUrl("http://localhost/users/1")); |
58 | | - } |
59 | | - |
60 | | - @Test |
61 | | - public void whenAddingNewUserWithoutName_thenErrorStatusCodeAndResponse() throws Exception { |
62 | | - WebsiteUser user = new WebsiteUser(); |
63 | | - user.setEmail("john.doe@john.com"); |
64 | | - |
65 | | - mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) |
66 | | - .andExpect(status().isNotAcceptable()) |
67 | | - .andExpect(redirectedUrl(null)); |
68 | | - } |
69 | | - |
70 | | - @Test |
71 | | - public void whenAddingNewUserWithEmptyName_thenErrorStatusCodeAndResponse() throws Exception { |
72 | | - WebsiteUser user = new WebsiteUser(); |
73 | | - user.setEmail("john.doe@john.com"); |
74 | | - user.setName(""); |
75 | | - mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) |
76 | | - .andExpect(status().isNotAcceptable()) |
77 | | - .andExpect(redirectedUrl(null)); |
78 | | - } |
79 | | - |
80 | | - @Test |
81 | | - public void whenAddingNewUserWithoutEmail_thenErrorStatusCodeAndResponse() throws Exception { |
82 | | - WebsiteUser user = new WebsiteUser(); |
83 | | - user.setName("John Doe"); |
84 | | - |
85 | | - mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) |
86 | | - .andExpect(status().isNotAcceptable()) |
87 | | - .andExpect(redirectedUrl(null)); |
88 | | - } |
89 | | - |
90 | | - @Test |
91 | | - public void whenAddingNewUserWithEmptyEmail_thenErrorStatusCodeAndResponse() throws Exception { |
92 | | - WebsiteUser user = new WebsiteUser(); |
93 | | - user.setName("John Doe"); |
94 | | - user.setEmail(""); |
95 | | - mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) |
96 | | - .andExpect(status().isNotAcceptable()) |
97 | | - .andExpect(redirectedUrl(null)); |
98 | | - } |
99 | | - |
100 | | -} |
| 1 | +package com.baeldung.validator; |
| 2 | + |
| 3 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 4 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 5 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; |
| 6 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 7 | +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; |
| 8 | + |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.test.context.SpringBootTest; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 16 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 17 | +import org.springframework.test.web.servlet.MockMvc; |
| 18 | +import org.springframework.web.context.WebApplicationContext; |
| 19 | + |
| 20 | +import com.baeldung.SpringDataRestApplication; |
| 21 | +import com.baeldung.models.WebsiteUser; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | + |
| 24 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 25 | +@SpringBootTest(classes = SpringDataRestApplication.class) |
| 26 | +@WebAppConfiguration |
| 27 | +public class SpringDataRestValidatorTest { |
| 28 | + public static final String URL = "http://localhost"; |
| 29 | + |
| 30 | + private MockMvc mockMvc; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + protected WebApplicationContext wac; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setup() { |
| 37 | + mockMvc = webAppContextSetup(wac).build(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void whenStartingApplication_thenCorrectStatusCode() throws Exception { |
| 42 | + mockMvc.perform(get("/users")).andExpect(status().is2xxSuccessful()); |
| 43 | + }; |
| 44 | + |
| 45 | + @Test |
| 46 | + public void whenAddingNewCorrectUser_thenCorrectStatusCodeAndResponse() throws Exception { |
| 47 | + WebsiteUser user = new WebsiteUser(); |
| 48 | + user.setEmail("john.doe@john.com"); |
| 49 | + user.setName("John Doe"); |
| 50 | + |
| 51 | + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().is2xxSuccessful()).andExpect(redirectedUrl("http://localhost/users/1")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void whenAddingNewUserWithoutName_thenErrorStatusCodeAndResponse() throws Exception { |
| 56 | + WebsiteUser user = new WebsiteUser(); |
| 57 | + user.setEmail("john.doe@john.com"); |
| 58 | + |
| 59 | + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void whenAddingNewUserWithEmptyName_thenErrorStatusCodeAndResponse() throws Exception { |
| 64 | + WebsiteUser user = new WebsiteUser(); |
| 65 | + user.setEmail("john.doe@john.com"); |
| 66 | + user.setName(""); |
| 67 | + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void whenAddingNewUserWithoutEmail_thenErrorStatusCodeAndResponse() throws Exception { |
| 72 | + WebsiteUser user = new WebsiteUser(); |
| 73 | + user.setName("John Doe"); |
| 74 | + |
| 75 | + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void whenAddingNewUserWithEmptyEmail_thenErrorStatusCodeAndResponse() throws Exception { |
| 80 | + WebsiteUser user = new WebsiteUser(); |
| 81 | + user.setName("John Doe"); |
| 82 | + user.setEmail(""); |
| 83 | + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null)); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments