@Test public void shouldCreateUser() throws Exception { final String name = "Martha"; final String currency = "€"; final UserResource userResource = new UserResource(name, currency); when(userServiceMock.create(anyString(), anyString())).thenAnswer(invocation -> new User().setId(1L) .setName(invocation.getArgument(0)) .setCurrency(invocation.getArgument(1)) ); final MockHttpServletRequestBuilder requestBuilder = post("/users").accept(MediaType.APPLICATION_JSON) .content(JsonHelper.toJson(userResource)).contentType(MediaType.APPLICATION_JSON); final MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn(); final MockHttpServletResponse response = mvcResult.getResponse(); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); final UserResource result = JsonHelper.fromJson(response.getContentAsString(), UserResource.class); assertThat(result.getName()).isEqualTo(name); assertThat(result.getCurrency()).isEqualTo(currency); verify(userServiceMock).create(name, currency); }
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<UserResource> create(@RequestBody @Valid UserResource userResource) { User user = userService.create(userResource.getName(), userResource.getCurrency()); final UserResource createdUserResource = userResourceAssembler.toResource(user); return new ResponseEntity<>(createdUserResource, HttpStatus.OK); }
UserController { @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<UserResource> create(@RequestBody @Valid UserResource userResource) { User user = userService.create(userResource.getName(), userResource.getCurrency()); final UserResource createdUserResource = userResourceAssembler.toResource(user); return new ResponseEntity<>(createdUserResource, HttpStatus.OK); } }
UserController { @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<UserResource> create(@RequestBody @Valid UserResource userResource) { User user = userService.create(userResource.getName(), userResource.getCurrency()); final UserResource createdUserResource = userResourceAssembler.toResource(user); return new ResponseEntity<>(createdUserResource, HttpStatus.OK); } @Autowired  UserController(UserService userService, UserResourceAssembler userResourceAssembler); }
UserController { @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<UserResource> create(@RequestBody @Valid UserResource userResource) { User user = userService.create(userResource.getName(), userResource.getCurrency()); final UserResource createdUserResource = userResourceAssembler.toResource(user); return new ResponseEntity<>(createdUserResource, HttpStatus.OK); } @Autowired  UserController(UserService userService, UserResourceAssembler userResourceAssembler); @GetMapping Resources<UserResource> list(); @GetMapping("/{userId}") Resource<UserResource> get(@PathVariable("userId") long userId); @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<UserResource> create(@RequestBody @Valid UserResource userResource); }
UserController { @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<UserResource> create(@RequestBody @Valid UserResource userResource) { User user = userService.create(userResource.getName(), userResource.getCurrency()); final UserResource createdUserResource = userResourceAssembler.toResource(user); return new ResponseEntity<>(createdUserResource, HttpStatus.OK); } @Autowired  UserController(UserService userService, UserResourceAssembler userResourceAssembler); @GetMapping Resources<UserResource> list(); @GetMapping("/{userId}") Resource<UserResource> get(@PathVariable("userId") long userId); @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<UserResource> create(@RequestBody @Valid UserResource userResource);  }