Skip to content

Commit cf76e12

Browse files
author
Eugen Paraschiv
committed
using the Spring MVC testing support properly
1 parent 30552c8 commit cf76e12

6 files changed

Lines changed: 45 additions & 47 deletions

File tree

spring-boot/src/main/java/org/baeldung/repository/UserRepository.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import org.springframework.data.repository.query.Param;
1111
import org.springframework.scheduling.annotation.Async;
1212
import org.springframework.stereotype.Repository;
13-
import org.springframework.transaction.annotation.Propagation;
14-
import org.springframework.transaction.annotation.Transactional;
1513

1614
import java.util.Collection;
1715
import java.util.List;

spring-boot/src/test/java/com/baeldung/shutdown/ShutdownApplicationTest.java

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

3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5+
36
import org.junit.Before;
47
import org.junit.Ignore;
58
import org.junit.Test;
@@ -12,10 +15,6 @@
1215
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1316
import org.springframework.web.context.WebApplicationContext;
1417

15-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
16-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
17-
18-
1918
@RunWith(SpringJUnit4ClassRunner.class)
2019
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
2120
@AutoConfigureMockMvc
@@ -36,8 +35,6 @@ public void setup() {
3635
@Ignore
3736
public void givenBootApp_whenShutdownEndpoint_thenExit() throws Exception {
3837

39-
mockMvc.perform(
40-
post("/shutdown"))
41-
.andExpect(status().isOk());
38+
mockMvc.perform(post("/shutdown")).andExpect(status().isOk());
4239
}
4340
}

spring-boot/src/test/java/com/baeldung/toggle/ToggleIntegrationTest.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
package com.baeldung.toggle;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6+
37
import org.junit.Before;
48
import org.junit.Test;
59
import org.junit.runner.RunWith;
610
import org.springframework.beans.factory.annotation.Autowired;
711
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
812
import org.springframework.boot.test.context.SpringBootTest;
9-
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
1013
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1114
import org.springframework.test.web.servlet.MockMvc;
1215
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1316
import org.springframework.web.context.WebApplicationContext;
1417

15-
import static org.junit.Assert.assertEquals;
16-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
17-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
18-
1918
@RunWith(SpringJUnit4ClassRunner.class)
20-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = ToggleApplication.class)
19+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = ToggleApplication.class)
2120
@AutoConfigureMockMvc
2221
public class ToggleIntegrationTest {
2322

2423
@Autowired
25-
SalaryService salaryService;
26-
27-
@Autowired
28-
EmployeeRepository employeeRepository;
24+
private EmployeeRepository employeeRepository;
2925

3026
@Autowired
3127
private MockMvc mockMvc;

spring-boot/src/test/java/org/baeldung/converter/controller/StringToEmployeeConverterControllerIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.baeldung.boot.Application;
1818

1919
@RunWith(SpringRunner.class)
20-
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
20+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = Application.class)
2121
@AutoConfigureMockMvc
2222
public class StringToEmployeeConverterControllerIntegrationTest {
2323

spring-boot/src/test/java/org/baeldung/demo/boottest/EmployeeRestControllerIntegrationTest.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
11
package org.baeldung.demo.boottest;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.hamcrest.CoreMatchers.is;
5+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
6+
import static org.hamcrest.Matchers.hasSize;
7+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
8+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
10+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
13+
14+
import java.io.IOException;
15+
import java.util.List;
16+
317
import org.baeldung.demo.DemoApplication;
4-
import org.baeldung.demo.boottest.Employee;
5-
import org.baeldung.demo.boottest.EmployeeRepository;
618
import org.junit.After;
719
import org.junit.Test;
820
import org.junit.runner.RunWith;
921
import org.springframework.beans.factory.annotation.Autowired;
1022
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
1123
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1224
import org.springframework.boot.test.context.SpringBootTest;
13-
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
1425
import org.springframework.http.MediaType;
1526
import org.springframework.test.context.junit4.SpringRunner;
1627
import org.springframework.test.web.servlet.MockMvc;
1728

18-
import java.io.IOException;
19-
import java.util.List;
20-
21-
import static org.assertj.core.api.Assertions.assertThat;
22-
import static org.hamcrest.CoreMatchers.is;
23-
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
24-
import static org.hamcrest.Matchers.hasSize;
25-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
27-
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
28-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
29-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
30-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31-
3229
@RunWith(SpringRunner.class)
33-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = DemoApplication.class)
30+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = DemoApplication.class)
3431
@AutoConfigureMockMvc
3532
// @TestPropertySource(locations = "classpath:application-integrationtest.properties")
3633
@AutoConfigureTestDatabase
@@ -58,14 +55,22 @@ public void whenValidInput_thenCreateEmployee() throws IOException, Exception {
5855

5956
@Test
6057
public void givenEmployees_whenGetEmployees_thenStatus200() throws Exception {
61-
6258
createTestEmployee("bob");
6359
createTestEmployee("alex");
6460

65-
mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$", hasSize(greaterThanOrEqualTo(2))))
66-
.andExpect(jsonPath("$[0].name", is("bob"))).andExpect(jsonPath("$[1].name", is("alex")));
61+
// @formatter:off
62+
mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON))
63+
.andDo(print())
64+
.andExpect(status().isOk())
65+
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
66+
.andExpect(jsonPath("$", hasSize(greaterThanOrEqualTo(2))))
67+
.andExpect(jsonPath("$[0].name", is("bob")))
68+
.andExpect(jsonPath("$[1].name", is("alex")));
69+
// @formatter:on
6770
}
6871

72+
//
73+
6974
private void createTestEmployee(String name) {
7075
Employee emp = new Employee(name);
7176
repository.saveAndFlush(emp);

spring-custom-aop/spring-custom-aop/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithoutServletComponentIntegrationTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package com.baeldung.annotation.servletcomponentscan;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
7+
import javax.servlet.FilterRegistration;
8+
import javax.servlet.ServletContext;
9+
310
import org.junit.Test;
411
import org.junit.runner.RunWith;
512
import org.springframework.beans.factory.annotation.Autowired;
@@ -11,13 +18,8 @@
1118
import org.springframework.test.context.TestPropertySource;
1219
import org.springframework.test.context.junit4.SpringRunner;
1320

14-
import javax.servlet.FilterRegistration;
15-
import javax.servlet.ServletContext;
16-
17-
import static org.junit.Assert.*;
18-
1921
@RunWith(SpringRunner.class)
20-
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootPlainApp.class)
22+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = SpringBootPlainApp.class)
2123
@AutoConfigureMockMvc
2224
@TestPropertySource(properties = { "security.basic.enabled=false" })
2325
public class SpringBootWithoutServletComponentIntegrationTest {

0 commit comments

Comments
 (0)