Skip to content

Commit aed7a46

Browse files
BAEL-175 - moving to mvc java module
1 parent 34678f2 commit aed7a46

13 files changed

Lines changed: 39 additions & 326 deletions

File tree

‎spring-mvc-java/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@
111111
<version>${mockito.version}</version>
112112
<scope>test</scope>
113113
</dependency>
114+
<dependency>
115+
<groupId>com.jayway.jsonpath</groupId>
116+
<artifactId>json-path</artifactId>
117+
<version>2.2.0</version>
118+
</dependency>
114119
<dependency>
115120
<groupId>org.springframework</groupId>
116121
<artifactId>spring-test</artifactId>

spring-mvc-test/src/main/java/com/baeldung/spring/bean/Greeting.java renamed to spring-mvc-java/src/main/java/com/baeldung/model/Greeting.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.spring.bean;
1+
package com.baeldung.model;
22

33
public class Greeting {
44
private int id;

spring-mvc-test/src/main/java/com/baeldung/spring/ApplicationConfig.java renamed to spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.spring;
1+
package com.baeldung.spring.web.config;
22

33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.ComponentScan;
@@ -12,7 +12,7 @@
1212

1313
@EnableWebMvc
1414
@Configuration
15-
@ComponentScan(basePackages = { "com.baeldung.spring.controller" })
15+
@ComponentScan(basePackages = { "com.baeldung.web.controller" })
1616
public class ApplicationConfig extends WebMvcConfigurerAdapter {
1717

1818
public ApplicationConfig() {

spring-mvc-test/src/main/java/com/baeldung/spring/controller/GreetController.java renamed to spring-mvc-java/src/main/java/com/baeldung/web/controller/GreetController.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
package com.baeldung.spring.controller;
1+
package com.baeldung.web.controller;
22

3+
import com.baeldung.model.Greeting;
34
import org.springframework.stereotype.Controller;
4-
import org.springframework.web.bind.annotation.PathVariable;
5-
import org.springframework.web.bind.annotation.RequestMapping;
6-
import org.springframework.web.bind.annotation.RequestMethod;
7-
import org.springframework.web.bind.annotation.RequestParam;
8-
import org.springframework.web.bind.annotation.ResponseBody;
9-
10-
import com.baeldung.spring.bean.Greeting;
5+
import org.springframework.web.bind.annotation.*;
116

127
@Controller
138
public class GreetController {
File renamed without changes.

spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerIntegrationTest.java renamed to spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerIntegrationTest.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
package com.baeldung.spring.controller;
1+
package com.baeldung.web.controller;
22

3+
4+
import com.baeldung.spring.web.config.ApplicationConfig;
35
import org.junit.Assert;
46
import org.junit.Before;
57
import org.junit.Test;
@@ -11,28 +13,28 @@
1113
import org.springframework.test.context.web.WebAppConfiguration;
1214
import org.springframework.test.web.servlet.MockMvc;
1315
import org.springframework.test.web.servlet.MvcResult;
16+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
1417
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
1518
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
16-
17-
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
1819
import org.springframework.web.context.WebApplicationContext;
1920

20-
import com.baeldung.spring.ApplicationConfig;
21-
22-
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
23-
2421
import javax.servlet.ServletContext;
2522

23+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
24+
2625
@RunWith(SpringJUnit4ClassRunner.class)
2726
@WebAppConfiguration
28-
@ContextConfiguration(classes = { ApplicationConfig.class })
27+
@ContextConfiguration(classes = {ApplicationConfig.class})
2928
public class GreetControllerIntegrationTest {
3029

3130
@Autowired
3231
private WebApplicationContext wac;
3332

3433
private MockMvc mockMvc;
3534

35+
private static final String CONTENT_TYPE = "application/json";
36+
37+
3638
@Before
3739
public void setup() throws Exception {
3840
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
@@ -54,36 +56,36 @@ public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Ex
5456
@Test
5557
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
5658
MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/greet")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")).andReturn();
57-
Assert.assertEquals("application/json;charset=UTF-8", mvcResult.getResponse().getContentType());
59+
Assert.assertEquals(CONTENT_TYPE, mvcResult.getResponse().getContentType());
5860
}
5961

6062
@Test
6163
public void givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() throws Exception {
62-
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
64+
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
6365
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John!!!"));
6466
}
6567

6668
@Test
6769
public void givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() throws Exception {
68-
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/{name}", "Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
70+
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithPathVariable/{name}", "Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
6971
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World Doe!!!"));
7072
}
7173

7274
@Test
7375
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
7476
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
75-
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!"));
77+
.andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!"));
7678
}
7779

7880
@Test
7981
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
80-
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
82+
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
8183
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
8284
}
8385

8486
@Test
8587
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
8688
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
87-
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
89+
.andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
8890
}
8991
}

spring-mvc-test/src/test/java/com/baeldung/spring/controller/GreetControllerTest.java renamed to spring-mvc-java/src/test/java/com/baeldung/web/controller/GreetControllerTest.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.spring.controller;
1+
package com.baeldung.web.controller;
22

33
import org.junit.Before;
44
import org.junit.Test;
@@ -9,14 +9,12 @@
99

1010
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1111
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
12-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
13-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
14-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
15-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
1613

1714
public class GreetControllerTest {
1815

1916
private MockMvc mockMvc;
17+
private static final String CONTENT_TYPE = "application/json";
2018

2119
@Before
2220
public void setup() {
@@ -30,34 +28,34 @@ public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() throws Ex
3028

3129
@Test
3230
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
33-
this.mockMvc.perform(get("/greet")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World!!!"));
31+
this.mockMvc.perform(get("/greet")).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World!!!"));
3432
}
3533

3634
@Test
3735
public void givenGreetURIWithPathVariable_whenMockMVC_thenVerifyResponse() throws Exception {
38-
this.mockMvc.perform(get("/greetWithPathVariable/John")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World John!!!"));
36+
this.mockMvc.perform(get("/greetWithPathVariable/John")).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John!!!"));
3937
}
4038

4139
@Test
4240
public void givenGreetURIWithPathVariable_2_whenMockMVC_thenVerifyResponse() throws Exception {
43-
this.mockMvc.perform(get("/greetWithPathVariable/{name}", "Doe")).andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8")).andExpect(jsonPath("$.message").value("Hello World Doe!!!"));
41+
this.mockMvc.perform(get("/greetWithPathVariable/{name}", "Doe")).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World Doe!!!"));
4442
}
4543

4644
@Test
4745
public void givenGreetURIWithQueryParameter_whenMockMVC_thenVerifyResponse() throws Exception {
48-
this.mockMvc.perform(MockMvcRequestBuilders.get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
49-
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!"));
46+
this.mockMvc.perform(get("/greetWithQueryVariable").param("name", "John Doe")).andDo(print()).andExpect(status().isOk())
47+
.andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John Doe!!!"));
5048
}
5149

5250
@Test
5351
public void givenGreetURIWithPost_whenMockMVC_thenVerifyResponse() throws Exception {
54-
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
55-
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!"));
52+
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPost")).andDo(print()).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE))
53+
.andExpect(jsonPath("$.message").value("Hello World!!!"));
5654
}
5755

5856
@Test
5957
public void givenGreetURIWithPostAndFormData_whenMockMVC_thenVerifyResponse() throws Exception {
60-
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
61-
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1));
58+
this.mockMvc.perform(MockMvcRequestBuilders.post("/greetWithPostAndFormData").param("id", "1").param("name", "John Doe")).andDo(print()).andExpect(status().isOk())
59+
.andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$.message").value("Hello World John Doe!!!")).andExpect(jsonPath("$.id").value(1));
6260
}
6361
}

‎spring-mvc-test/README‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)