BAEL-2736 rest assured spring mock mvc (#6466)

* Include rest-assured.spring-mock-mvc dependency

BAEL-2736

* Rest Assured Spring Mock MVC Tests

BAEL-2736
This commit is contained in:
Trevor Gowing
2019-03-25 15:29:04 +02:00
committed by KevinGilmore
parent d84dedbb95
commit fda8a29eb6
8 changed files with 226 additions and 10 deletions
@@ -0,0 +1,17 @@
package com.baeldung.restassured.learner;
class Course {
private String code;
public Course() {
}
Course(String code) {
this.code = code;
}
String getCode() {
return code;
}
}
@@ -0,0 +1,31 @@
package com.baeldung.restassured.learner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
@RestController
@RequestMapping(path = "/courses")
public class CourseController {
private final CourseService courseService;
public CourseController(CourseService courseService) {
this.courseService = courseService;
}
@GetMapping(produces = APPLICATION_JSON_UTF8_VALUE)
public Collection<Course> getCourses() {
return courseService.getCourses();
}
@GetMapping(path = "/{code}", produces = APPLICATION_JSON_UTF8_VALUE)
public Course getCourse(@PathVariable String code) {
return courseService.getCourse(code);
}
}
@@ -0,0 +1,18 @@
package com.baeldung.restassured.learner;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice(assignableTypes = CourseController.class)
public class CourseControllerExceptionHandler extends ResponseEntityExceptionHandler {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(CourseNotFoundException.class)
@SuppressWarnings("ThrowablePrintedToSystemOut")
public void handleCourseNotFoundException(CourseNotFoundException cnfe) {
System.out.println(cnfe);
}
}
@@ -0,0 +1,8 @@
package com.baeldung.restassured.learner;
class CourseNotFoundException extends RuntimeException {
CourseNotFoundException(String code) {
super(code);
}
}
@@ -0,0 +1,27 @@
package com.baeldung.restassured.learner;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
@Service
class CourseService {
private static final Map<String, Course> COURSE_MAP = new ConcurrentHashMap<>();
static {
Course wizardry = new Course("Wizardry");
COURSE_MAP.put(wizardry.getCode(), wizardry);
}
Collection<Course> getCourses() {
return COURSE_MAP.values();
}
Course getCourse(String code) {
return Optional.ofNullable(COURSE_MAP.get(code)).orElseThrow(() -> new CourseNotFoundException(code));
}
}