Java集成测试服务方法

时间:2018-06-25 09:18:15

标签: java spring web-services integration-testing

我想使用给定的时间为我的一种服务方法创建集成测试。我的方法对我来说有点复杂,所以我有一些方法可以测试这种方法: 这是我的服务方法:

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());
    }

    private Set<TeamDto> mapTeams(Person person) {
        return person.getTeams().stream().map(t -> modelMapper.map(t, TeamDto.class)).collect(Collectors.toSet());
    }

但是我想测试方法peopelWithTeamsAssigned。我有团队和人,但我有方法只返回分配了人员的这支团队。 其实我的测试看起来像这样:

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceIntegrationTest {

    @Autowired
    private PersonService personService;
    @Autowired
    private TeamService teamService;
    @Autowired
    private EntityManager entityManager;

    @Test
    public void shouldAutowireServiceImplementation() {
        assertThat(personService).isNotNull();
    }

    @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

    }
}

我有一个问题要问,什么时候可以?如何创建给定此方法,我需要创建一个列表。

0 个答案:

没有答案