DefaultGroupService implements GroupService { @Override public List<Group> groups(User user) { final List<Membership> memberships = membershipRepository.findByUser(user); final List<Group> groups = new ArrayList<>(); for (Membership membership : memberships) { groups.add(membership.getGroup()); } return groups; } DefaultGroupService(GroupRepository groupRepository, MembershipRepository membershipRepository); @Override Group createGroup(String name); @Override void deleteGroup(Group group); @Override void join(Group group, User user); @Override void leave(Group group, User user); @Override List<User> members(Group group); @Override List<Group> groups(User user);  }
@Test public void testListingUserGroups() { Group group = new Group(); group.setName("My Group"); group = groupRepository.save(group); final User user = userService.createUser("milad", "milad@domain.com", "123456"); final Membership membership = new Membership(); membership.setUser(user); membership.setGroup(group); membershipRepository.save(membership); final List<Group> groups = service.groups(user); assertThat(groups, is(notNullValue())); assertThat(groups, hasSize(1)); assertThat(groups.get(0), is(notNullValue())); assertThat(groups.get(0).getId(), is(group.getId())); }