@Test public void getUser() { final User mockedUser = new User(); mockedUser.setId("user-1"); mockedUser.setUsername("user1"); mockedUser.setFirstName("First Name 1"); mockedUser.setLastName("Last Name 1"); mockedUser.setEmail("user1@test.com"); when(userServiceMock.getUser(anyString())).then(new Answer<User>() { @Override public User answer(InvocationOnMock invocation) throws Throwable { String argUserId = (String) invocation.getArguments()[0]; assertEquals("Service user ID should match the requested user ID", argUserId, mockedUser.getId()); return mockedUser; } }); User responseUser = resource().path("/api/users/" + mockedUser.getId()) .accept(MediaType.APPLICATION_JSON).get(User.class); assertEquals("Response user should match the mocked user", mockedUser, responseUser); verify(userServiceMock).getUser(anyString()); }
@GET @Path("/{userId}") @Produces(MediaType.APPLICATION_JSON) public User getUser(@PathParam("userId") String userId) { return userService.getUser(userId); }
UserResource { @GET @Path("/{userId}") @Produces(MediaType.APPLICATION_JSON) public User getUser(@PathParam("userId") String userId) { return userService.getUser(userId); } }
UserResource { @GET @Path("/{userId}") @Produces(MediaType.APPLICATION_JSON) public User getUser(@PathParam("userId") String userId) { return userService.getUser(userId); } @Inject  UserResource(UserService userService); }
UserResource { @GET @Path("/{userId}") @Produces(MediaType.APPLICATION_JSON) public User getUser(@PathParam("userId") String userId) { return userService.getUser(userId); } @Inject  UserResource(UserService userService); @GET @Produces(MediaType.APPLICATION_JSON) Collection<User> listUsers(); @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) User createUser(User user); @GET @Path("/{userId}") @Produces(MediaType.APPLICATION_JSON) User getUser(@PathParam("userId") String userId); @PUT @Path("/{userId}") @Consumes(MediaType.APPLICATION_JSON) Response updateUser(@PathParam("userId") String userId, User user); @DELETE @Path("/{userId}") Response deleteUser(@PathParam("userId") String userId); @PUT @Path("/{userId}/password") @Consumes(MediaType.APPLICATION_JSON) Response updateUserPassword(@PathParam("userId") String userId,
            PasswordChange passwordChange); }
UserResource { @GET @Path("/{userId}") @Produces(MediaType.APPLICATION_JSON) public User getUser(@PathParam("userId") String userId) { return userService.getUser(userId); } @Inject  UserResource(UserService userService); @GET @Produces(MediaType.APPLICATION_JSON) Collection<User> listUsers(); @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) User createUser(User user); @GET @Path("/{userId}") @Produces(MediaType.APPLICATION_JSON) User getUser(@PathParam("userId") String userId); @PUT @Path("/{userId}") @Consumes(MediaType.APPLICATION_JSON) Response updateUser(@PathParam("userId") String userId, User user); @DELETE @Path("/{userId}") Response deleteUser(@PathParam("userId") String userId); @PUT @Path("/{userId}/password") @Consumes(MediaType.APPLICATION_JSON) Response updateUserPassword(@PathParam("userId") String userId,
            PasswordChange passwordChange);  }