DefaultGroupService implements GroupService { @Override public void join(Group group, User user) { if (membershipRepository.findByUserAndGroup(user, group) != null) { return; } final Membership membership = new Membership(); membership.setGroup(group); membership.setUser(user); membershipRepository.save(membership); } 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 testEstablishingMembership() { Group group = new Group(); group.setName("My Group"); group = groupRepository.save(group); final User user = userService.createUser("milad", "milad@domain.com", "123456"); service.join(group, user); assertThat(membershipRepository.count(), is(1L)); final Membership membership = membershipRepository.findAll().get(0); assertThat(membership, is(notNullValue())); assertThat(membership.getGroup(), is(notNullValue())); assertThat(membership.getGroup().getId(), is(group.getId())); assertThat(membership.getUser(), is(notNullValue())); assertThat(membership.getUser().getId(), is(user.getId())); }