Estoy haciendo test unitarios en java, pero resulta que en el codigo del test el objeto whiteListRepository
siempre me trae un nullPointerException
(despues del when
) porque al parecer no se logra mockear bien, que podria ser?
Controller:
@Slf4j
@RestController
@RequestMapping(path = "/api/v1/match-whitelist")
public class WhitelistController {
@Autowired
private WhitelistServiceImpl whitelistService;
@GetMapping()
public ResponseEntity<WhitelistResponseDTO> findById(@RequestParam(name = "id", required = true) Long id) {
log.info("Calling findById with {}", id);
return ResponseEntity.ok(whitelistService.findById(id));
}
@PostMapping()
public ResponseEntity<WhitelistResponseDTO> save(@Valid @RequestBody WhitelistAddRequestDTO whiteListAddRequestDTO) {
log.info("Calling save with {}", whiteListAddRequestDTO);
return ResponseEntity.ok(whitelistService.save(whiteListAddRequestDTO));
}
}
Service:
@Service
public class WhitelistServiceImpl implements WhitelistService {
public WhitelistServiceImpl(WhiteListRepository whiteListRepository, WhitelistMapper whitelistMapper) {
this.whiteListRepository = whiteListRepository;
this.whitelistMapper = whitelistMapper;
}
@Value("${api.match.expired.whitelist}")
private Long timeExpired;
private WhiteListRepository whiteListRepository;
private WhitelistMapper whitelistMapper;
@Transactional(readOnly = true)
public WhitelistResponseDTO findById(Long id) {
WhiteList whiteList = whiteListRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
return whitelistMapper.toDTO(whiteList);
}
}
Repo:
@Repository("whiteListRepository")
public interface WhiteListRepository extends JpaRepository<WhiteList, Long> {
public Optional<WhiteList> findById(@Param("id") Long id);
}
Test:
public class WhitelistServiceTest {
@InjectMocks
private WhitelistServiceImpl whitelistServiceImpl;
private WhiteList whiteList;
@Mock
private WhiteListRepository whiteListRepository;
@Mock
private WhitelistMapper whitelistMapper;
private static final WhitelistStatus STATUS_ACTIVE = WhitelistStatus.ACTIVE;
private static final String AFIP_CODE = "1111111";
private static final String CUIT = "20275892249";
private static final String EMPLOYEE_ID = "L1005987";
private static final String SEVEN_DAYS_AGO = "L1005987";
@BeforeEach
public void setUp() {
whiteListRepository = mock(WhiteListRepository.class);
whitelistMapper = mock(WhitelistMapper.class);
whitelistServiceImpl = new WhitelistServiceImpl(whiteListRepository, whitelistMapper);
//MockitoAnnotations.initMocks(this);
}
@Test
public void shouldAddExceptionLowerOrEqualThan15Days() {
doReturn(Optional.ofNullable(WhiteList.builder()
.id(1L)
.cuit(CUIT)
.afipCode(AFIP_CODE)
.employeeId(EMPLOYEE_ID)
.status(STATUS_ACTIVE)
.createdAt(mockingLowerOrEqualThan15Days())
.updateAt(mockingToday())
.build())).when(whiteListRepository).findByStatusAndAfipCodeAndCuit(STATUS_ACTIVE, AFIP_CODE, CUIT);
WhiteList whiteList = builder();
WhitelistResponseDTO expected = whitelistMapper.toDTO(whiteList);
WhitelistResponseDTO result = whitelistMapper.toDTO(builderExpected());
Assertions.assertEquals(expected, result);
}
}