Skip to content

Commit

Permalink
Fix tests for JSON error rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Syer committed Oct 27, 2014
1 parent 4cd3bf1 commit f39d497
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void addErrorDetails(Map<String, Object> errorAttributes,
}
}
Object message = getAttribute(requestAttributes, "javax.servlet.error.message");
if (message != null || errorAttributes.get("message") == null) {
if ((message != null || errorAttributes.get("message") == null) && !(error instanceof BindingResult)) {
errorAttributes.put("message", message == null ? "No message available"
: message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
Expand All @@ -41,6 +42,7 @@
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractView;
Expand Down Expand Up @@ -73,8 +75,21 @@ public void testErrorForMachineClient() throws Exception {
"http://localhost:" + this.port, Map.class);
assertThat(entity.getBody().toString(), endsWith("status=500, "
+ "error=Internal Server Error, "
+ "exception=java.lang.IllegalStateException, " + "message=Expected!, "
+ "path=/}"));
+ "exception=java.lang.IllegalStateException, "
+ "message=Server Error, " + "path=/}"));
}

@Test
@SuppressWarnings("rawtypes")
public void testErrorForAnnotatedException() throws Exception {
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/annotated", Map.class);
assertThat(
entity.getBody().toString(),
endsWith("status=400, "
+ "error=Bad Request, "
+ "exception=org.springframework.boot.autoconfigure.web.BasicErrorControllerIntegrationTests$TestConfiguration$Errors$ExpectedException, "
+ "message=Expected!, " + "path=/annotated}"));
}

@Test
Expand Down Expand Up @@ -124,13 +139,24 @@ public String home() {
throw new IllegalStateException("Expected!");
}

@RequestMapping("/annotated")
public String annotated() {
throw new ExpectedException();
}

@RequestMapping("/bind")
public String bind(HttpServletRequest request) throws Exception {
BindException error = new BindException(this, "test");
error.rejectValue("foo", "bar.error");
throw error;
}

@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Expected!")
@SuppressWarnings("serial")
private static class ExpectedException extends RuntimeException {

}

}

}
Expand Down

0 comments on commit f39d497

Please sign in to comment.