Невозможно покрыть покрытие кода для потоков и фильтров Java 7 - PullRequest
0 голосов
/ 29 мая 2020

Как написать Mockito для кода ниже, специально для строк 1 и 6?

boolean present = employeeLanuageRefs.stream().anyMatch(x -> x.getTechnologyCd().equalsIgnoreCase(dto.getTechnologyCd()));  //Line-1
if (present) {
    throw new ResourceNotFoundException("Technology Code "+dto.getTechnologyCd() + " is already assigned to employee "+employee.getemployeeName());
}

// Check employee already have any primary Technology exists
Optional<employeeLanuageRef> optional = employeeLanuageRefs.stream()
        .filter(x -> "Y".equalsIgnoreCase(x.getPrimaryTechnologySw()))  //Line-6
        .findFirst();

employeeLanuageRef ref = null;
if(optional.isPresent()) {
    ref = optional.get();
}

// check if User trying to access another Primary Technology for employee
if(ref != null && "Y".equalsIgnoreCase(dto.getPrimaryTechnologySw())) {
    throw new ResourceNotFoundException("employee already have Primary Technology available");
}else {
    assignTechnologyToemployee(employeeLanuageRefs, dto, employee);
}

Эквивалентный тестовый пример

@Test(expected = ResourceNotFoundException.class)
public void testEmployee() {
    when(employeeTechMappingDto.getTechnologyCd()).thenReturn("es");
    when(employeeTechMappingDto.getPrimaryTechnologySw()).thenReturn("Y");

    // check if Technology code exists in Technology collection
    when(technologyRepository.findByTechnologyCd(any())).thenReturn(lang);

    when(technologyRefsSet.stream()).thenReturn(streamemployeeTechRef);
    when(streamemployeeTechRef.anyMatch(any())).thenReturn(true);
    when(countryMock.getCountrytechnologyRefs()).thenReturn(technologyRefsSet);

    countryService.saveemployeeTechuageMapping(employeeTechMappingDto);
}
...