Skip to content

Commit 50820a6

Browse files
authored
Merge pull request #8616 from kwoyke/BAEL-21543
BAEL-21543: Fix some of the failing integration tests after Spring Boot upgrade
2 parents 5905edf + 8d663f4 commit 50820a6

6 files changed

Lines changed: 32 additions & 43 deletions

File tree

spring-boot/src/test/java/com/baeldung/beanvalidation/application/UserControllerIntegrationTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import com.baeldung.beanvalidation.application.controllers.UserController;
44
import com.baeldung.beanvalidation.application.repositories.UserRepository;
5-
6-
import java.nio.charset.Charset;
7-
import static org.assertj.core.api.Assertions.assertThat;
85
import org.hamcrest.core.Is;
96
import org.junit.Test;
107
import org.junit.runner.RunWith;
@@ -18,6 +15,10 @@
1815
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
1916
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
2017

18+
import java.nio.charset.Charset;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
2122
@RunWith(SpringRunner.class)
2223
@WebMvcTest
2324
@AutoConfigureMockMvc
@@ -40,9 +41,9 @@ public void whenUserControllerInjected_thenNotNull() throws Exception {
4041
@Test
4142
public void whenGetRequestToUsers_thenCorrectResponse() throws Exception {
4243
mockMvc.perform(MockMvcRequestBuilders.get("/users")
43-
.contentType(MediaType.APPLICATION_JSON_UTF8))
44+
.contentType(MediaType.APPLICATION_JSON))
4445
.andExpect(MockMvcResultMatchers.status().isOk())
45-
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
46+
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
4647

4748
}
4849

@@ -52,7 +53,7 @@ public void whenPostRequestToUsersAndValidUser_thenCorrectResponse() throws Exce
5253
String user = "{\"name\": \"bob\", \"email\" : \"bob@domain.com\"}";
5354
mockMvc.perform(MockMvcRequestBuilders.post("/users")
5455
.content(user)
55-
.contentType(MediaType.APPLICATION_JSON_UTF8))
56+
.contentType(MediaType.APPLICATION_JSON))
5657
.andExpect(MockMvcResultMatchers.status().isOk())
5758
.andExpect(MockMvcResultMatchers.content().contentType(textPlainUtf8));
5859
}
@@ -62,9 +63,9 @@ public void whenPostRequestToUsersAndInValidUser_thenCorrectReponse() throws Exc
6263
String user = "{\"name\": \"\", \"email\" : \"bob@domain.com\"}";
6364
mockMvc.perform(MockMvcRequestBuilders.post("/users")
6465
.content(user)
65-
.contentType(MediaType.APPLICATION_JSON_UTF8))
66+
.contentType(MediaType.APPLICATION_JSON))
6667
.andExpect(MockMvcResultMatchers.status().isBadRequest())
6768
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Is.is("Name is mandatory")))
68-
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
69+
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
6970
}
7071
}

spring-mvc-basics-3/src/test/java/com/baeldung/SpringBootApplicationIntegrationTest.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1717
import org.springframework.web.context.WebApplicationContext;
1818

19-
import java.nio.charset.Charset;
20-
2119
import static org.hamcrest.Matchers.equalTo;
2220
import static org.hamcrest.Matchers.hasSize;
2321
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@@ -39,31 +37,23 @@ public void setupMockMvc() {
3937

4038
@Test
4139
public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throws Exception {
42-
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
43-
44-
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$", hasSize(4)));
40+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$", hasSize(4)));
4541
}
4642

4743
@Test
4844
public void givenRequestHasBeenMade_whenMeetsFindByDateOfGivenConditions_thenCorrect() throws Exception {
49-
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
50-
51-
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType))
45+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
5246
.andExpect(jsonPath("$.id", equalTo(1)));
5347
}
5448

5549
@Test
5650
public void givenRequestHasBeenMade_whenMeetsFindByModeOfGivenConditions_thenCorrect() throws Exception {
57-
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
58-
59-
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name())).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$.id", equalTo(1)));
51+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name())).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.id", equalTo(1)));
6052
}
6153

6254
@Test
6355
public void givenRequestHasBeenMade_whenMeetsFindByVersionOfGivenConditions_thenCorrect() throws Exception {
64-
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
65-
66-
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType))
56+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
6757
.andExpect(jsonPath("$.id", equalTo(1)));
6858
}
6959
}

spring-mvc-basics/src/test/java/com/baeldung/web/controller/EmployeeControllerContentNegotiationIntegrationTest.java

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

3-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
5-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6-
73
import org.junit.jupiter.api.Test;
84
import org.springframework.beans.factory.annotation.Autowired;
95
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@@ -12,6 +8,10 @@
128
import org.springframework.http.MediaType;
139
import org.springframework.test.web.servlet.MockMvc;
1410

11+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
13+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
14+
1515
@SpringBootTest
1616
@AutoConfigureMockMvc
1717
public class EmployeeControllerContentNegotiationIntegrationTest {
@@ -23,7 +23,7 @@ public class EmployeeControllerContentNegotiationIntegrationTest {
2323
public void whenEndpointUsingJsonSuffixCalled_thenJsonResponseObtained() throws Exception {
2424
this.mockMvc.perform(get("/employee/1.json"))
2525
.andExpect(status().isOk())
26-
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE));
26+
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
2727
}
2828

2929
@Test
@@ -37,7 +37,7 @@ public void whenEndpointUsingXmlSuffixCalled_thenXmlResponseObtained() throws Ex
3737
public void whenEndpointUsingJsonParameterCalled_thenJsonResponseObtained() throws Exception {
3838
this.mockMvc.perform(get("/employee/1?mediaType=json"))
3939
.andExpect(status().isOk())
40-
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE));
40+
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
4141
}
4242

4343
@Test
@@ -51,7 +51,7 @@ public void whenEndpointUsingXmlParameterCalled_thenXmlResponseObtained() throws
5151
public void whenEndpointUsingJsonAcceptHeaderCalled_thenJsonResponseObtained() throws Exception {
5252
this.mockMvc.perform(get("/employee/1").header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE))
5353
.andExpect(status().isOk())
54-
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE));
54+
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
5555
}
5656

5757
@Test

spring-mvc-basics/src/test/java/com/baeldung/web/controller/SimpleBookControllerIntegrationTest.java

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

3-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
5-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
6-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
7-
83
import org.junit.jupiter.api.BeforeEach;
94
import org.junit.jupiter.api.Test;
5+
import org.springframework.http.MediaType;
106
import org.springframework.test.web.servlet.MockMvc;
117
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
128

9+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
10+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
11+
1312
public class SimpleBookControllerIntegrationTest {
1413

1514
private MockMvc mockMvc;
16-
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
1715

1816
@BeforeEach
1917
public void setup() {
@@ -25,7 +23,7 @@ public void givenBookId_whenMockMVC_thenVerifyResponse() throws Exception {
2523
this.mockMvc
2624
.perform(get("/books/42"))
2725
.andExpect(status().isOk())
28-
.andExpect(content().contentType(CONTENT_TYPE))
26+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
2927
.andExpect(jsonPath("$.id").value(42));
3028
}
3129

spring-mvc-basics/src/test/java/com/baeldung/web/controller/SimpleBookRestControllerIntegrationTest.java

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

3-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
5-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
6-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
7-
83
import org.junit.jupiter.api.BeforeEach;
94
import org.junit.jupiter.api.Test;
5+
import org.springframework.http.MediaType;
106
import org.springframework.test.web.servlet.MockMvc;
117
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
128

9+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
10+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
11+
1312
public class SimpleBookRestControllerIntegrationTest {
1413

1514
private MockMvc mockMvc;
16-
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
1715

1816
@BeforeEach
1917
public void setup() {
@@ -25,7 +23,7 @@ public void givenBookId_whenMockMVC_thenVerifyResponse() throws Exception {
2523
this.mockMvc
2624
.perform(get("/books-rest/42"))
2725
.andExpect(status().isOk())
28-
.andExpect(content().contentType(CONTENT_TYPE))
26+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
2927
.andExpect(jsonPath("$.id").value(42));
3028
}
3129

testing-modules/rest-assured/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@
215215
<rest-assured-json-schema-validator.version>3.0.1</rest-assured-json-schema-validator.version>
216216

217217
<scribejava.version>2.5.3</scribejava.version>
218+
219+
<spring-boot.version>2.1.9.RELEASE</spring-boot.version>
218220
</properties>
219221

220222
</project>

0 commit comments

Comments
 (0)