Skip to content

Commit 000b6a7

Browse files
committed
Polish @ResponseStatus javadoc and StatusAssertionTests
1 parent f3db6b9 commit 000b6a7

File tree

3 files changed

+83
-40
lines changed

3 files changed

+83
-40
lines changed

spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/resultmatches/StatusAssertionTests.java

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,51 +20,58 @@
2020
import java.lang.annotation.RetentionPolicy;
2121

2222
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.TestInstance;
2324

2425
import org.springframework.core.annotation.AliasFor;
2526
import org.springframework.http.HttpStatus;
26-
import org.springframework.stereotype.Controller;
2727
import org.springframework.test.web.reactive.server.WebTestClient;
2828
import org.springframework.test.web.servlet.client.MockMvcWebTestClient;
29+
import org.springframework.web.bind.annotation.ExceptionHandler;
2930
import org.springframework.web.bind.annotation.RequestMapping;
30-
import org.springframework.web.bind.annotation.ResponseBody;
3131
import org.springframework.web.bind.annotation.ResponseStatus;
32+
import org.springframework.web.bind.annotation.RestController;
3233

3334
import static org.hamcrest.Matchers.equalTo;
35+
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
3436
import static org.springframework.http.HttpStatus.BAD_REQUEST;
3537
import static org.springframework.http.HttpStatus.CREATED;
3638
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
39+
import static org.springframework.http.HttpStatus.I_AM_A_TEAPOT;
3740
import static org.springframework.http.HttpStatus.NOT_IMPLEMENTED;
3841

3942
/**
4043
* {@link MockMvcWebTestClient} equivalent of the MockMvc
4144
* {@link org.springframework.test.web.servlet.samples.standalone.resultmatchers.StatusAssertionTests}.
4245
*
4346
* @author Rossen Stoyanchev
47+
* @author Sam Brannen
4448
*/
45-
public class StatusAssertionTests {
49+
@TestInstance(PER_CLASS)
50+
class StatusAssertionTests {
4651

4752
private final WebTestClient testClient =
4853
MockMvcWebTestClient.bindToController(new StatusController()).build();
4954

5055

5156
@Test
52-
public void testStatusInt() {
53-
testClient.get().uri("/created").exchange().expectStatus().isEqualTo(201);
54-
testClient.get().uri("/createdWithComposedAnnotation").exchange().expectStatus().isEqualTo(201);
55-
testClient.get().uri("/badRequest").exchange().expectStatus().isEqualTo(400);
57+
void statusInt() {
58+
testClient.get().uri("/teaPot").exchange().expectStatus().isEqualTo(I_AM_A_TEAPOT.value());
59+
testClient.get().uri("/created").exchange().expectStatus().isEqualTo(CREATED.value());
60+
testClient.get().uri("/createdWithComposedAnnotation").exchange().expectStatus().isEqualTo(CREATED.value());
61+
testClient.get().uri("/badRequest").exchange().expectStatus().isEqualTo(BAD_REQUEST.value());
62+
testClient.get().uri("/throwsException").exchange().expectStatus().isEqualTo(I_AM_A_TEAPOT.value());
5663
}
5764

5865
@Test
59-
public void testHttpStatus() {
66+
void httpStatus() {
6067
testClient.get().uri("/created").exchange().expectStatus().isCreated();
6168
testClient.get().uri("/createdWithComposedAnnotation").exchange().expectStatus().isCreated();
6269
testClient.get().uri("/badRequest").exchange().expectStatus().isBadRequest();
6370
}
6471

6572
@Test
66-
public void testMatcher() {
67-
testClient.get().uri("/badRequest").exchange().expectStatus().value(equalTo(400));
73+
void matcher() {
74+
testClient.get().uri("/badRequest").exchange().expectStatus().value(equalTo(BAD_REQUEST.value()));
6875
}
6976

7077

@@ -80,26 +87,41 @@ public void testMatcher() {
8087
HttpStatus status() default INTERNAL_SERVER_ERROR;
8188
}
8289

83-
@Controller
90+
@RestController
91+
@ResponseStatus(I_AM_A_TEAPOT)
8492
private static class StatusController {
8593

94+
@RequestMapping("/teaPot")
95+
void teaPot() {
96+
}
97+
8698
@RequestMapping("/created")
8799
@ResponseStatus(CREATED)
88-
public @ResponseBody void created(){
100+
void created(){
89101
}
90102

91103
@Get(path = "/createdWithComposedAnnotation", status = CREATED)
92-
public @ResponseBody void createdWithComposedAnnotation() {
104+
void createdWithComposedAnnotation() {
93105
}
94106

95107
@RequestMapping("/badRequest")
96108
@ResponseStatus(code = BAD_REQUEST, reason = "Expired token")
97-
public @ResponseBody void badRequest(){
109+
void badRequest(){
98110
}
99111

100112
@RequestMapping("/notImplemented")
101113
@ResponseStatus(NOT_IMPLEMENTED)
102-
public @ResponseBody void notImplemented(){
114+
void notImplemented(){
115+
}
116+
117+
@RequestMapping("/throwsException")
118+
@ResponseStatus(NOT_IMPLEMENTED)
119+
void throwsException() {
120+
throw new IllegalStateException();
121+
}
122+
123+
@ExceptionHandler
124+
void exceptionHandler(IllegalStateException ex) {
103125
}
104126
}
105127

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/StatusAssertionTests.java

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,20 +20,23 @@
2020
import java.lang.annotation.RetentionPolicy;
2121

2222
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.TestInstance;
2324

2425
import org.springframework.core.annotation.AliasFor;
2526
import org.springframework.http.HttpStatus;
26-
import org.springframework.stereotype.Controller;
2727
import org.springframework.test.web.servlet.MockMvc;
28+
import org.springframework.web.bind.annotation.ExceptionHandler;
2829
import org.springframework.web.bind.annotation.RequestMapping;
29-
import org.springframework.web.bind.annotation.ResponseBody;
3030
import org.springframework.web.bind.annotation.ResponseStatus;
31+
import org.springframework.web.bind.annotation.RestController;
3132

3233
import static org.hamcrest.Matchers.endsWith;
3334
import static org.hamcrest.Matchers.equalTo;
35+
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
3436
import static org.springframework.http.HttpStatus.BAD_REQUEST;
3537
import static org.springframework.http.HttpStatus.CREATED;
3638
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
39+
import static org.springframework.http.HttpStatus.I_AM_A_TEAPOT;
3740
import static org.springframework.http.HttpStatus.NOT_IMPLEMENTED;
3841
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3942
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -45,39 +48,40 @@
4548
* @author Rossen Stoyanchev
4649
* @author Sam Brannen
4750
*/
48-
public class StatusAssertionTests {
51+
@TestInstance(PER_CLASS)
52+
class StatusAssertionTests {
4953

5054
private final MockMvc mockMvc = standaloneSetup(new StatusController()).build();
5155

5256
@Test
53-
public void testStatusInt() throws Exception {
54-
this.mockMvc.perform(get("/created")).andExpect(status().is(201));
55-
this.mockMvc.perform(get("/createdWithComposedAnnotation")).andExpect(status().is(201));
56-
this.mockMvc.perform(get("/badRequest")).andExpect(status().is(400));
57-
}
58-
59-
@Test
60-
public void testHttpStatus() throws Exception {
57+
void httpStatus() throws Exception {
6158
this.mockMvc.perform(get("/created")).andExpect(status().isCreated());
6259
this.mockMvc.perform(get("/createdWithComposedAnnotation")).andExpect(status().isCreated());
6360
this.mockMvc.perform(get("/badRequest")).andExpect(status().isBadRequest());
6461
}
6562

6663
@Test
67-
public void testMatcher() throws Exception {
68-
this.mockMvc.perform(get("/badRequest")).andExpect(status().is(equalTo(400)));
64+
void statusCode() throws Exception {
65+
this.mockMvc.perform(get("/teaPot")).andExpect(status().is(I_AM_A_TEAPOT.value()));
66+
this.mockMvc.perform(get("/created")).andExpect(status().is(CREATED.value()));
67+
this.mockMvc.perform(get("/createdWithComposedAnnotation")).andExpect(status().is(CREATED.value()));
68+
this.mockMvc.perform(get("/badRequest")).andExpect(status().is(BAD_REQUEST.value()));
69+
this.mockMvc.perform(get("/throwsException")).andExpect(status().is(I_AM_A_TEAPOT.value()));
6970
}
7071

7172
@Test
72-
public void testReasonEqualTo() throws Exception {
73-
this.mockMvc.perform(get("/badRequest")).andExpect(status().reason("Expired token"));
73+
void statusCodeWithMatcher() throws Exception {
74+
this.mockMvc.perform(get("/badRequest")).andExpect(status().is(equalTo(BAD_REQUEST.value())));
75+
}
7476

75-
// Hamcrest matchers...
76-
this.mockMvc.perform(get("/badRequest")).andExpect(status().reason(equalTo("Expired token")));
77+
@Test
78+
void reason() throws Exception {
79+
this.mockMvc.perform(get("/badRequest")).andExpect(status().reason("Expired token"));
7780
}
7881

7982
@Test
80-
public void testReasonMatcher() throws Exception {
83+
void reasonWithMatcher() throws Exception {
84+
this.mockMvc.perform(get("/badRequest")).andExpect(status().reason(equalTo("Expired token")));
8185
this.mockMvc.perform(get("/badRequest")).andExpect(status().reason(endsWith("token")));
8286
}
8387

@@ -94,26 +98,41 @@ public void testReasonMatcher() throws Exception {
9498
HttpStatus status() default INTERNAL_SERVER_ERROR;
9599
}
96100

97-
@Controller
101+
@RestController
102+
@ResponseStatus(I_AM_A_TEAPOT)
98103
private static class StatusController {
99104

105+
@RequestMapping("/teaPot")
106+
void teaPot() {
107+
}
108+
100109
@RequestMapping("/created")
101110
@ResponseStatus(CREATED)
102-
public @ResponseBody void created(){
111+
void created() {
103112
}
104113

105114
@Get(path = "/createdWithComposedAnnotation", status = CREATED)
106-
public @ResponseBody void createdWithComposedAnnotation() {
115+
void createdWithComposedAnnotation() {
107116
}
108117

109118
@RequestMapping("/badRequest")
110119
@ResponseStatus(code = BAD_REQUEST, reason = "Expired token")
111-
public @ResponseBody void badRequest(){
120+
void badRequest() {
112121
}
113122

114123
@RequestMapping("/notImplemented")
115124
@ResponseStatus(NOT_IMPLEMENTED)
116-
public @ResponseBody void notImplemented(){
125+
void notImplemented() {
126+
}
127+
128+
@RequestMapping("/throwsException")
129+
@ResponseStatus(NOT_IMPLEMENTED)
130+
void throwsException() {
131+
throw new IllegalStateException();
132+
}
133+
134+
@ExceptionHandler
135+
void exceptionHandler(IllegalStateException ex) {
117136
}
118137
}
119138

spring-web/src/main/java/org/springframework/web/bind/annotation/ResponseStatus.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979

8080
/**
8181
* The <em>reason</em> to be used for the response.
82+
* <p>Defaults to an empty string which will be ignored. Set the reason to a
83+
* non-empty value to have it used for the response.
8284
* @see javax.servlet.http.HttpServletResponse#sendError(int, String)
8385
*/
8486
String reason() default "";

0 commit comments

Comments
 (0)