简体   繁体   中英

Why lenient solves Unneccessary Stubbing exception in given example

I have read previous questions but there is not exact answer pointing my questions directly. My code is below:

@Service
@RequiredArgsConstructor
public class CityServiceImpl implements CityService {
    private final CityRepository cityRepo;
    private final CityMapper cityMapper;
@Override
public CityDto findById(Integer id) {
    City city = cityRepo.findById(id)
            .orElseThrow(() -> new NotFoundException(NotFoundException.Domain.CITY));
    return CityMapper.INSTANCE.cityToCityDto(city);

 }
}

My test class is as below:

@ExtendWith(MockitoExtension.class)
public class CityServiceTest {
  @Mock
  CityRepository cityRepository;

  @Mock
  CityMapper cityMapper;

  @InjectMocks
  CityServiceImpl cityService;
  City city;

  @BeforeEach
  public void init() {
    city = new City();
  }
  @Test
  void findById_Success() {
    Integer given = 1;

    CityDto expected = new CityDto();

    when(cityRepository.findById(1)).thenReturn(Optional.of(city));
    when(cityMapper.cityToCityDto(city)).thenReturn(expected);

    CityDto actual = cityService.findById(given);

    assertEquals(expected, actual);

  }
}

I got an error pointing to this line

when(cityMapper.cityToCityDto(city)).thenReturn(expected);

Unnecessary stubbings detected. Clean & maintainable test code requires zero unnecessary code. Following stubbings are unnecessary

I got an aswer that when i use lenient.when(cityMapper.cityToCityDto(city)).thenReturn(expected); or annotation of lenient works fine. But what is the logic behind it.

Why lenient solves Unneccessary Stubbing exception in given example?

It looks like your mocked cityMapper is never being used, that's why you see the exception.

Replace CityMapper.INSTANCE with cityMapper in the findById method and the UnnecessaryStubbingException should go away. If your code still does what it should, however, I cannot decide from what I see.

The real dependency is CityMapper cityMapper. In findById_Success you are calling

cityMapper.cityToCityDto(city)

But, if you see the real method is not calling that. Instead

CityMapper.INSTANCE.cityToCityDto(city)

has been used. Change any of them, it should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM