Hello I have a method adding persons to teams. I want to write a test for this method but i'm newbie in junit/mockito test so i have many problems: This is my add method:
@Transactional
public void addPersonsToTeams(Long teamId, Long personId) {
Assert.notNull(personId, "Object can't be null!");
Assert.notNull(teamId, "Object can't be null!");
try {
Person person = personRepository.getOne(personId);
Team team = teamRepository.getOne(teamId);
person.getTeams().add(team);
personRepository.save(person);
} catch (Exception e) {
throw new CreateEntityException();
}
}
There is relation between this two entities(people/teams) And this is my test code but it doesn't work:
@Test
public void shouldAddPersonToTeam(){
Team team = new Team(1l, "TestCase1", "Description1", "Krakow", 12);
Person person = new Person(1L, "jan", "mucha", "krakow", "email1@onet.com", "Programing", "Developer");
teamService.createTeam(mapper.map(team, TeamDto.class));
personService.addPerson(mapper.map(person, PersonDto.class));
teamService.addPersonsToTeams(team.getId(), person.getId());
verify(teamRepository, times(1)).save(team);
verify(personRepository, times(1)).save(person);
}
Mock konfiguration :
public class TeamServiceTest {
private TeamService teamService;
private ModelMapper mapper;
private PersonService personService;
@Mock
private TeamRepository teamRepository;
private PersonRepository personRepository; //-this is never assigned :/
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.mapper = new ModelMapper();
teamService = new TeamService(teamRepository, this.mapper);
personService = new PersonService(personRepository, this.mapper);
}