This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration("failing-mockito-config.xml") | |
public class BeanWiringTest { | |
@Autowired | |
SomeClass someClass; | |
@Autowired | |
SomeDependency someDependencyMock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jayway.example; | |
public interface Delegate { | |
String doSomething(); | |
String doSomethingElse(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Example { | |
private Delegate delegate; | |
Example(Delegate delegate) { | |
this.delegate = delegate; | |
} | |
public void doIt() { | |
delegate.execute(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@XmlRootElement | |
public class ErrorMessage { | |
private List<String> errors; | |
public ErrorMessage() { | |
} | |
public ErrorMessage(List<String> errors) { | |
this.errors = errors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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("[email protected]"))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) { |