Skip to content

Commit fda8a29

Browse files
trevorgowingKevinGilmore
authored andcommitted
BAEL-2736 rest assured spring mock mvc (eugenp#6466)
* Include rest-assured.spring-mock-mvc dependency BAEL-2736 * Rest Assured Spring Mock MVC Tests BAEL-2736
1 parent d84dedb commit fda8a29

File tree

8 files changed

+226
-10
lines changed

8 files changed

+226
-10
lines changed

testing-modules/rest-assured/pom.xml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
<groupId>org.springframework.boot</groupId>
2020
<artifactId>spring-boot-starter-web</artifactId>
2121
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-json</artifactId>
25+
</dependency>
2226
<dependency>
2327
<groupId>org.springframework.boot</groupId>
2428
<artifactId>spring-boot-starter-test</artifactId>
@@ -97,7 +101,7 @@
97101

98102
<dependency>
99103
<groupId>joda-time</groupId>
100-
<artifactId>joda-time</artifactId>
104+
<artifactId>joda-time</artifactId>
101105
</dependency>
102106

103107
<dependency>
@@ -143,15 +147,6 @@
143147
<artifactId>wiremock</artifactId>
144148
<version>${wiremock.version}</version>
145149
</dependency>
146-
<dependency>
147-
<groupId>io.rest-assured</groupId>
148-
<artifactId>rest-assured</artifactId>
149-
<scope>test</scope>
150-
</dependency>
151-
<dependency>
152-
<groupId>io.rest-assured</groupId>
153-
<artifactId>json-schema-validator</artifactId>
154-
</dependency>
155150
<dependency>
156151
<groupId>com.github.fge</groupId>
157152
<artifactId>json-schema-validator</artifactId>
@@ -167,6 +162,23 @@
167162
<artifactId>commons-collections</artifactId>
168163
<version>${commons-collections.version}</version>
169164
</dependency>
165+
166+
<!-- Rest Assured Dependencies-->
167+
<dependency>
168+
<groupId>io.rest-assured</groupId>
169+
<artifactId>rest-assured</artifactId>
170+
<scope>test</scope>
171+
</dependency>
172+
<dependency>
173+
<groupId>io.rest-assured</groupId>
174+
<artifactId>spring-mock-mvc</artifactId>
175+
<scope>test</scope>
176+
</dependency>
177+
<dependency>
178+
<groupId>io.rest-assured</groupId>
179+
<artifactId>json-schema-validator</artifactId>
180+
<scope>test</scope>
181+
</dependency>
170182
</dependencies>
171183

172184
<properties>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.restassured.learner;
2+
3+
class Course {
4+
5+
private String code;
6+
7+
public Course() {
8+
}
9+
10+
Course(String code) {
11+
this.code = code;
12+
}
13+
14+
String getCode() {
15+
return code;
16+
}
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.restassured.learner;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import java.util.Collection;
9+
10+
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
11+
12+
@RestController
13+
@RequestMapping(path = "/courses")
14+
public class CourseController {
15+
16+
private final CourseService courseService;
17+
18+
public CourseController(CourseService courseService) {
19+
this.courseService = courseService;
20+
}
21+
22+
@GetMapping(produces = APPLICATION_JSON_UTF8_VALUE)
23+
public Collection<Course> getCourses() {
24+
return courseService.getCourses();
25+
}
26+
27+
@GetMapping(path = "/{code}", produces = APPLICATION_JSON_UTF8_VALUE)
28+
public Course getCourse(@PathVariable String code) {
29+
return courseService.getCourse(code);
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.restassured.learner;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ControllerAdvice;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.ResponseStatus;
7+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
8+
9+
@ControllerAdvice(assignableTypes = CourseController.class)
10+
public class CourseControllerExceptionHandler extends ResponseEntityExceptionHandler {
11+
12+
@ResponseStatus(HttpStatus.NOT_FOUND)
13+
@ExceptionHandler(CourseNotFoundException.class)
14+
@SuppressWarnings("ThrowablePrintedToSystemOut")
15+
public void handleCourseNotFoundException(CourseNotFoundException cnfe) {
16+
System.out.println(cnfe);
17+
}
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.restassured.learner;
2+
3+
class CourseNotFoundException extends RuntimeException {
4+
5+
CourseNotFoundException(String code) {
6+
super(code);
7+
}
8+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.restassured.learner;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import java.util.Collection;
6+
import java.util.Map;
7+
import java.util.Optional;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
10+
@Service
11+
class CourseService {
12+
13+
private static final Map<String, Course> COURSE_MAP = new ConcurrentHashMap<>();
14+
15+
static {
16+
Course wizardry = new Course("Wizardry");
17+
COURSE_MAP.put(wizardry.getCode(), wizardry);
18+
}
19+
20+
Collection<Course> getCourses() {
21+
return COURSE_MAP.values();
22+
}
23+
24+
Course getCourse(String code) {
25+
return Optional.ofNullable(COURSE_MAP.get(code)).orElseThrow(() -> new CourseNotFoundException(code));
26+
}
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.restassured.learner;
2+
3+
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
4+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
5+
import static org.springframework.http.HttpStatus.NOT_FOUND;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
import org.springframework.web.context.WebApplicationContext;
14+
15+
import io.restassured.module.mockmvc.RestAssuredMockMvc;
16+
17+
@RunWith(SpringRunner.class)
18+
@SpringBootTest(webEnvironment = RANDOM_PORT)
19+
public class CourseControllerIntegrationTest {
20+
21+
@Autowired
22+
private WebApplicationContext webApplicationContext;
23+
24+
@Before
25+
public void initialiseRestAssuredMockMvcWebApplicationContext() {
26+
RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
27+
}
28+
29+
@Test
30+
public void givenNoMatchingCourseCodeWhenGetCourseThenRespondWithStatusNotFound() {
31+
String nonMatchingCourseCode = "nonMatchingCourseCode";
32+
33+
given()
34+
.when()
35+
.get("/courses/" + nonMatchingCourseCode)
36+
.then()
37+
.log().ifValidationFails()
38+
.statusCode(NOT_FOUND.value());
39+
}
40+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.restassured.learner;
2+
3+
import io.restassured.module.mockmvc.RestAssuredMockMvc;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.mockito.InjectMocks;
8+
import org.mockito.Mock;
9+
import org.mockito.junit.MockitoJUnitRunner;
10+
11+
import java.util.Collections;
12+
13+
import static io.restassured.http.ContentType.JSON;
14+
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
15+
import static org.hamcrest.Matchers.equalTo;
16+
import static org.hamcrest.Matchers.is;
17+
import static org.mockito.Mockito.when;
18+
import static org.springframework.http.HttpStatus.NOT_FOUND;
19+
import static org.springframework.http.HttpStatus.OK;
20+
21+
@RunWith(MockitoJUnitRunner.class)
22+
public class CourseControllerUnitTest {
23+
24+
@Mock
25+
private CourseService courseService;
26+
@InjectMocks
27+
private CourseController courseController;
28+
@InjectMocks
29+
private CourseControllerExceptionHandler courseControllerExceptionHandler;
30+
31+
@Before
32+
public void initialiseRestAssuredMockMvcStandalone() {
33+
RestAssuredMockMvc.standaloneSetup(courseController, courseControllerExceptionHandler);
34+
}
35+
36+
@Test
37+
public void givenNoExistingCoursesWhenGetCoursesThenRespondWithStatusOkAndEmptyArray() {
38+
when(courseService.getCourses()).thenReturn(Collections.emptyList());
39+
40+
given()
41+
.when()
42+
.get("/courses")
43+
.then()
44+
.log().ifValidationFails()
45+
.statusCode(OK.value())
46+
.contentType(JSON)
47+
.body(is(equalTo("[]")));
48+
}
49+
50+
@Test
51+
public void givenNoMatchingCoursesWhenGetCoursesThenRespondWithStatusNotFound() {
52+
String nonMatchingCourseCode = "nonMatchingCourseCode";
53+
54+
when(courseService.getCourse(nonMatchingCourseCode)).thenThrow(new CourseNotFoundException(nonMatchingCourseCode));
55+
56+
given()
57+
.when()
58+
.get("/courses/" + nonMatchingCourseCode)
59+
.then()
60+
.log().ifValidationFails()
61+
.statusCode(NOT_FOUND.value());
62+
}
63+
}

0 commit comments

Comments
 (0)