CtripUserService implements UserService { @Override public List<UserInfo> findByUserIds(List<String> userIds) { UserServiceRequest request = assembleFindUserRequest(Lists.newArrayList(userIds)); 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); @Override List<UserInfo> findByUserIds(List<String> userIds);  }
@Test public void testFindByUserIds() throws Exception { String someUserId = "someUserId"; String someName = "someName"; String someEmail = "someEmail"; String anotherUserId = "anotherUserId"; String anotherName = "anotherName"; String anotherEmail = "anotherEmail"; String someKeyword = "someKeyword"; 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.findByUserIds(Lists.newArrayList(someUserId, anotherUserId)); assertEquals(2, users.size()); compareUserInfoAndUserServiceResponse(someUserResponse, users.get(0)); compareUserInfoAndUserServiceResponse(anotherUserResponse, users.get(1)); }