### Question:
CtripUserService implements UserService { @Override public List<UserInfo> searchUsers(String keyword, int offset, int limit) { UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit); HttpEntity<UserServiceRequest> entity = new HttpEntity<>(request); ResponseEntity<Map<String, List<UserServiceResponse>>> response = restTemplate.exchange(portalConfig.userServiceUrl(), HttpMethod.POST, entity, responseType); if (!response.getBody().containsKey("result")) { return Collections.emptyList(); } List<UserInfo> result = Lists.newArrayList(); result.addAll( response.getBody().get("result").stream().map(this::transformUserServiceResponseToUserInfo) .collect(Collectors.toList())); return result; } CtripUserService(PortalConfig portalConfig); @Override List<UserInfo> searchUsers(String keyword, int offset, int limit); @Override UserInfo findByUserId(String userId); List<UserInfo> findByUserIds(List<String> userIds);  }### Answer:
@Test public void testSearchUsers() throws Exception { String someUserId = "someUserId"; String someName = "someName"; String someEmail = "someEmail"; String anotherUserId = "anotherUserId"; String anotherName = "anotherName"; String anotherEmail = "anotherEmail"; String someKeyword = "someKeyword"; int someOffset = 0; int someLimit = 10; CtripUserService.UserServiceResponse someUserResponse = assembleUserServiceResponse(someUserId, someName, someEmail); CtripUserService.UserServiceResponse anotherUserResponse = assembleUserServiceResponse(anotherUserId, anotherName, anotherEmail); Map<String, List<CtripUserService.UserServiceResponse>> resultMap = ImmutableMap.of("result", Lists.newArrayList(someUserResponse, anotherUserResponse)); ResponseEntity<Map<String, List<CtripUserService.UserServiceResponse>>> someResponse = new ResponseEntity<>(resultMap, HttpStatus.OK); when(restTemplate.exchange(eq(someUserServiceUrl), eq(HttpMethod.POST), any(HttpEntity.class), eq(someResponseType))).thenReturn(someResponse); List<UserInfo> users = ctripUserService.searchUsers(someKeyword, someOffset, someLimit); assertEquals(2, users.size()); compareUserInfoAndUserServiceResponse(someUserResponse, users.get(0)); compareUserInfoAndUserServiceResponse(anotherUserResponse, users.get(1)); }
@Test(expected = HttpClientErrorException.class) public void testSearchUsersWithError() throws Exception { when(restTemplate.exchange(eq(someUserServiceUrl), eq(HttpMethod.POST), any(HttpEntity.class), eq(someResponseType))) .thenThrow(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR)); String someKeyword = "someKeyword"; int someOffset = 0; int someLimit = 10; ctripUserService.searchUsers(someKeyword, someOffset, someLimit); }