@Test public void shouldGetOneUser() throws Exception { when(userServiceMock.get(anyLong())).thenReturn(UserTestUtils.createBob()); final MockHttpServletRequestBuilder requestBuilder = get("/users/{id}", 1L).accept(MediaType.APPLICATION_JSON); final MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn(); final MockHttpServletResponse response = mvcResult.getResponse(); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); final UserResource userResource = JsonHelper.fromJson(response.getContentAsString(), UserResource.class); assertThat(userResource.getName()).isEqualTo("Bob"); assertThat(userResource.getCurrency()).isEqualTo("€"); }
@GetMapping("/{userId}") public Resource<UserResource> get(@PathVariable("userId") long userId) { User user = userService.get(userId); final UserResource userResource = userResourceAssembler.toResource(user); return new Resource<>(userResource); }
UserController { @GetMapping("/{userId}") public Resource<UserResource> get(@PathVariable("userId") long userId) { User user = userService.get(userId); final UserResource userResource = userResourceAssembler.toResource(user); return new Resource<>(userResource); } }
UserController { @GetMapping("/{userId}") public Resource<UserResource> get(@PathVariable("userId") long userId) { User user = userService.get(userId); final UserResource userResource = userResourceAssembler.toResource(user); return new Resource<>(userResource); } @Autowired  UserController(UserService userService, UserResourceAssembler userResourceAssembler); }
UserController { @GetMapping("/{userId}") public Resource<UserResource> get(@PathVariable("userId") long userId) { User user = userService.get(userId); final UserResource userResource = userResourceAssembler.toResource(user); return new Resource<>(userResource); } @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 { @GetMapping("/{userId}") public Resource<UserResource> get(@PathVariable("userId") long userId) { User user = userService.get(userId); final UserResource userResource = userResourceAssembler.toResource(user); return new Resource<>(userResource); } @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);  }