我想为我的服务方法编写一个集成测试,但是与此有关。我还有其他想要像模式一样使用的测试,但是对我来说仍然太难了,因为在这种方法中,我有不同种类的return元素: 这是我的方法:
public List<PersonWithTeamsDto> peopelWithTeamsAssigned() {
List<Person> people = personRepository.findByTeamsNotEmpty();
return people.stream()
.map(person -> {
PersonWithTeamsDto dto = modelMapper.map(person, PersonWithTeamsDto.class);
dto.setTeams(mapTeams(person));
return dto;
}).collect(Collectors.toList());
}
我想测试这个方法!! 我想做这样的事情:
public void shouldAddPersonToTeam() {
// Given
TeamDto teamDto = new TeamDto(null, "Janek", "local", "wawa", 6);
PersonDto personDto = new PersonDto(null, "janek", "mucha", "email1@onet.com", "krakow", "Programing", "Developer");
Team team = teamService.createTeam(teamDto);
Person person = personService.addPerson(personDto);
// When
final long personId = person.getId();
teamService.addPersonsToTeams(team.getId(), personId);
entityManager.flush();
entityManager.clear();
// Then
Optional<Team> teamFromService = teamService.findTeamEntityById(team.getId());
assertThat(teamFromService.isPresent()).isTrue();
Set<Person> persons = teamFromService.get().getPersons();
assertThat(persons).hasSize(1);
assertThat(persons.stream().filter(person1 -> person1.getId() == personId).findAny()).isPresent();
}
但是实际上我有这个:
@Test
@Transactional
public void shouldReturnPeopleWithTeamsAssigned(){
// Given
TeamDto teamDto = new TeamDto(null, "Janek", "local", "wawa", 6);
PersonDto personDto = new PersonDto(null, "janek", "mucha", "email1@onet.com", "krakow", "Programing", "Developer");
Team team = teamService.createTeam(teamDto);
Person person = personService.addPerson(personDto);
//When
teamService.addPersonsToTeams(team.getId(),person.getId());
personService.peopelWithTeamsAssigned();
entityManager.flush();
entityManager.clear();
//Then
List<PersonWithTeamsDto> personWithTeamsDtos = personService.peopelWithTeamsAssigned();
assertThat(personWithTeamsDtos.contains();
}