Polish tests

This commit is contained in:
Rossen Stoyanchev 2018-03-09 19:57:13 -05:00
parent 107f8bb5fd
commit 3653a37e27
5 changed files with 21 additions and 22 deletions

View File

@ -20,7 +20,6 @@ import java.net.URI;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@ -29,6 +28,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
@ -44,7 +44,7 @@ public class JsonContentTests {
@Test
public void jsonContent() throws Exception {
public void jsonContent() {
this.client.get().uri("/persons")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
@ -53,7 +53,7 @@ public class JsonContentTests {
}
@Test
public void jsonPathIsEqualTo() throws Exception {
public void jsonPathIsEqualTo() {
this.client.get().uri("/persons")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
@ -64,9 +64,8 @@ public class JsonContentTests {
.jsonPath("$[2].name").isEqualTo("John");
}
@Test
// See https://stackoverflow.com/questions/49149376/webtestclient-check-that-jsonpath-contains-sub-string
public void jsonPathContainsSubstringViaRegularExpression() throws Exception {
@Test // https://stackoverflow.com/questions/49149376/webtestclient-check-that-jsonpath-contains-sub-string
public void jsonPathContainsSubstringViaRegex() {
this.client.get().uri("/persons/John")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
@ -78,7 +77,7 @@ public class JsonContentTests {
}
@Test
public void postJsonContent() throws Exception {
public void postJsonContent() {
this.client.post().uri("/persons")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.syncBody("{\"name\":\"John\"}")
@ -89,14 +88,15 @@ public class JsonContentTests {
@RestController
@RequestMapping("/persons")
static class PersonController {
@GetMapping("/persons")
@GetMapping
Flux<Person> getPersons() {
return Flux.just(new Person("Jane"), new Person("Jason"), new Person("John"));
}
@GetMapping("/persons/{name}")
@GetMapping("/{name}")
Person getPerson(@PathVariable String name) {
return new Person(name);
}

View File

@ -21,9 +21,8 @@ import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
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.RequestMethod;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@ -64,7 +63,7 @@ public class ExceptionHandlerTests {
@Controller
private static class PersonController {
@RequestMapping(value="/person/{name}", method=RequestMethod.GET)
@GetMapping("/person/{name}")
public String show(@PathVariable String name) {
if (name.equals("Clyde")) {
throw new IllegalArgumentException("simulated exception");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,9 +26,9 @@ import org.springframework.test.web.Person;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
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.RequestMethod;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@ -109,13 +109,13 @@ public class RedirectTests {
@Controller
private static class PersonController {
@RequestMapping(value="/persons/{name}", method=RequestMethod.GET)
@GetMapping("/persons/{name}")
public String getPerson(@PathVariable String name, Model model) {
model.addAttribute(new Person(name));
return "persons/index";
}
@RequestMapping(value="/persons", method=RequestMethod.POST)
@PostMapping
public String save(@Valid Person person, Errors errors, RedirectAttributes redirectAttrs) {
if (errors.hasErrors()) {
return "persons/add";
@ -125,7 +125,7 @@ public class RedirectTests {
return "redirect:/persons/{name}";
}
@RequestMapping(value="/people", method=RequestMethod.POST)
@PostMapping("/people")
public Object saveSpecial(@Valid Person person, Errors errors, RedirectAttributes redirectAttrs) {
if (errors.hasErrors()) {
return "persons/add";

View File

@ -30,9 +30,8 @@ import org.springframework.ui.Model;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.FixedContentNegotiationStrategy;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
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.RequestMethod;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@ -136,7 +135,7 @@ public class ViewResolutionTests {
@Controller
private static class PersonController {
@RequestMapping(value="/person/{name}", method=RequestMethod.GET)
@GetMapping("/person/{name}")
public String show(@PathVariable String name, Model model) {
Person person = new Person(name);
model.addAttribute(person);

View File

@ -28,6 +28,7 @@ import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -112,7 +113,7 @@ public class ModelAssertionTests {
return "view";
}
@RequestMapping(value="/persons", method=RequestMethod.POST)
@PostMapping("/persons")
public String create(@Valid Person person, BindingResult result, Model model) {
return "view";
}