Skip to content

Instantly share code, notes, and snippets.

Mattias Severson matsev

Block or report user

Report or block matsev

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@matsev
matsev / CustomResponseEntityExceptionHandler.java
Last active Jul 28, 2019
Generic response error handling using @ControllerAdvice
View CustomResponseEntityExceptionHandler.java
@ControllerAdvice
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size());
String error;
for (FieldError fieldError : fieldErrors) {
@matsev
matsev / GetTestUserAsJson.java
Created Sep 8, 2012
Spring Controller Tests 2.0
View GetTestUserAsJson.java
@Test
public void shouldGetTestUserAsJson() throws Exception {
mockMvc
.perform(get("/user/0")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("name", is("Test User")))
.andExpect(jsonPath("email", is("test.user@somewhere.com")));
}
@matsev
matsev / ErrorMessage.java
Last active Sep 28, 2018
Enhanced error feedback from a Spring Controller
View ErrorMessage.java
@XmlRootElement
public class ErrorMessage {
private List<String> errors;
public ErrorMessage() {
}
public ErrorMessage(List<String> errors) {
this.errors = errors;
View CreateUser.java
@RequestMapping(method = RequestMethod.POST,
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
@ResponseStatus(HttpStatus.CREATED)
void create(@RequestBody @Valid User user, HttpServletRequest request, HttpServletResponse response) {
long userId = userService.create(user);
String location = ServletUriComponentsBuilder.fromRequestUri(request).path("/{userid}")
.build()
.expand(userId).toUriString();
response.addHeader("Location", location);
}
@matsev
matsev / ConstructorExample.java
Created Feb 18, 2012
Mockito and Dependency Injection
View ConstructorExample.java
public class Example {
private Delegate delegate;
Example(Delegate delegate) {
this.delegate = delegate;
}
public void doIt() {
delegate.execute();
@matsev
matsev / Delegate.java
Created Dec 3, 2011
Using Mock Obects in Spring Integration Tests
View Delegate.java
package com.jayway.example;
public interface Delegate {
String doSomething();
String doSomethingElse();
}
@matsev
matsev / BeanWiringTest.java
Created Nov 27, 2011
Creating Spring FactoryBeans for Mock Objects
View BeanWiringTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("failing-mockito-config.xml")
public class BeanWiringTest {
@Autowired
SomeClass someClass;
@Autowired
SomeDependency someDependencyMock;
You can’t perform that action at this time.