Skip to content

Commit

Permalink
Polish @ResponseStatus javadoc and StatusAssertionTests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jun 8, 2021
1 parent f3db6b9 commit 000b6a7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
Expand All @@ -20,51 +20,58 @@
import java.lang.annotation.RetentionPolicy;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import org.springframework.core.annotation.AliasFor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.servlet.client.MockMvcWebTestClient;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.I_AM_A_TEAPOT;
import static org.springframework.http.HttpStatus.NOT_IMPLEMENTED;

/**
* {@link MockMvcWebTestClient} equivalent of the MockMvc
* {@link org.springframework.test.web.servlet.samples.standalone.resultmatchers.StatusAssertionTests}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
public class StatusAssertionTests {
@TestInstance(PER_CLASS)
class StatusAssertionTests {

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


@Test
public void testStatusInt() {
testClient.get().uri("/created").exchange().expectStatus().isEqualTo(201);
testClient.get().uri("/createdWithComposedAnnotation").exchange().expectStatus().isEqualTo(201);
testClient.get().uri("/badRequest").exchange().expectStatus().isEqualTo(400);
void statusInt() {
testClient.get().uri("/teaPot").exchange().expectStatus().isEqualTo(I_AM_A_TEAPOT.value());
testClient.get().uri("/created").exchange().expectStatus().isEqualTo(CREATED.value());
testClient.get().uri("/createdWithComposedAnnotation").exchange().expectStatus().isEqualTo(CREATED.value());
testClient.get().uri("/badRequest").exchange().expectStatus().isEqualTo(BAD_REQUEST.value());
testClient.get().uri("/throwsException").exchange().expectStatus().isEqualTo(I_AM_A_TEAPOT.value());
}

@Test
public void testHttpStatus() {
void httpStatus() {
testClient.get().uri("/created").exchange().expectStatus().isCreated();
testClient.get().uri("/createdWithComposedAnnotation").exchange().expectStatus().isCreated();
testClient.get().uri("/badRequest").exchange().expectStatus().isBadRequest();
}

@Test
public void testMatcher() {
testClient.get().uri("/badRequest").exchange().expectStatus().value(equalTo(400));
void matcher() {
testClient.get().uri("/badRequest").exchange().expectStatus().value(equalTo(BAD_REQUEST.value()));
}


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

@Controller
@RestController
@ResponseStatus(I_AM_A_TEAPOT)
private static class StatusController {

@RequestMapping("/teaPot")
void teaPot() {
}

@RequestMapping("/created")
@ResponseStatus(CREATED)
public @ResponseBody void created(){
void created(){
}

@Get(path = "/createdWithComposedAnnotation", status = CREATED)
public @ResponseBody void createdWithComposedAnnotation() {
void createdWithComposedAnnotation() {
}

@RequestMapping("/badRequest")
@ResponseStatus(code = BAD_REQUEST, reason = "Expired token")
public @ResponseBody void badRequest(){
void badRequest(){
}

@RequestMapping("/notImplemented")
@ResponseStatus(NOT_IMPLEMENTED)
public @ResponseBody void notImplemented(){
void notImplemented(){
}

@RequestMapping("/throwsException")
@ResponseStatus(NOT_IMPLEMENTED)
void throwsException() {
throw new IllegalStateException();
}

@ExceptionHandler
void exceptionHandler(IllegalStateException ex) {
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
Expand All @@ -20,20 +20,23 @@
import java.lang.annotation.RetentionPolicy;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import org.springframework.core.annotation.AliasFor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.I_AM_A_TEAPOT;
import static org.springframework.http.HttpStatus.NOT_IMPLEMENTED;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -45,39 +48,40 @@
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
public class StatusAssertionTests {
@TestInstance(PER_CLASS)
class StatusAssertionTests {

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

@Test
public void testStatusInt() throws Exception {
this.mockMvc.perform(get("/created")).andExpect(status().is(201));
this.mockMvc.perform(get("/createdWithComposedAnnotation")).andExpect(status().is(201));
this.mockMvc.perform(get("/badRequest")).andExpect(status().is(400));
}

@Test
public void testHttpStatus() throws Exception {
void httpStatus() throws Exception {
this.mockMvc.perform(get("/created")).andExpect(status().isCreated());
this.mockMvc.perform(get("/createdWithComposedAnnotation")).andExpect(status().isCreated());
this.mockMvc.perform(get("/badRequest")).andExpect(status().isBadRequest());
}

@Test
public void testMatcher() throws Exception {
this.mockMvc.perform(get("/badRequest")).andExpect(status().is(equalTo(400)));
void statusCode() throws Exception {
this.mockMvc.perform(get("/teaPot")).andExpect(status().is(I_AM_A_TEAPOT.value()));
this.mockMvc.perform(get("/created")).andExpect(status().is(CREATED.value()));
this.mockMvc.perform(get("/createdWithComposedAnnotation")).andExpect(status().is(CREATED.value()));
this.mockMvc.perform(get("/badRequest")).andExpect(status().is(BAD_REQUEST.value()));
this.mockMvc.perform(get("/throwsException")).andExpect(status().is(I_AM_A_TEAPOT.value()));
}

@Test
public void testReasonEqualTo() throws Exception {
this.mockMvc.perform(get("/badRequest")).andExpect(status().reason("Expired token"));
void statusCodeWithMatcher() throws Exception {
this.mockMvc.perform(get("/badRequest")).andExpect(status().is(equalTo(BAD_REQUEST.value())));
}

// Hamcrest matchers...
this.mockMvc.perform(get("/badRequest")).andExpect(status().reason(equalTo("Expired token")));
@Test
void reason() throws Exception {
this.mockMvc.perform(get("/badRequest")).andExpect(status().reason("Expired token"));
}

@Test
public void testReasonMatcher() throws Exception {
void reasonWithMatcher() throws Exception {
this.mockMvc.perform(get("/badRequest")).andExpect(status().reason(equalTo("Expired token")));
this.mockMvc.perform(get("/badRequest")).andExpect(status().reason(endsWith("token")));
}

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

@Controller
@RestController
@ResponseStatus(I_AM_A_TEAPOT)
private static class StatusController {

@RequestMapping("/teaPot")
void teaPot() {
}

@RequestMapping("/created")
@ResponseStatus(CREATED)
public @ResponseBody void created(){
void created() {
}

@Get(path = "/createdWithComposedAnnotation", status = CREATED)
public @ResponseBody void createdWithComposedAnnotation() {
void createdWithComposedAnnotation() {
}

@RequestMapping("/badRequest")
@ResponseStatus(code = BAD_REQUEST, reason = "Expired token")
public @ResponseBody void badRequest(){
void badRequest() {
}

@RequestMapping("/notImplemented")
@ResponseStatus(NOT_IMPLEMENTED)
public @ResponseBody void notImplemented(){
void notImplemented() {
}

@RequestMapping("/throwsException")
@ResponseStatus(NOT_IMPLEMENTED)
void throwsException() {
throw new IllegalStateException();
}

@ExceptionHandler
void exceptionHandler(IllegalStateException ex) {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@

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

0 comments on commit 000b6a7

Please sign in to comment.