Olá! Estou tentando fazer o teste da classe ValidacaoPetDisponivelTest e também da AdocaoServiceTest. Aparece um erro que o Mockit não consegue fazer o mock da classe. Pesquisei o que poderia ser, até mesmo baixei o código disponibilizado na aula mas não encontrei o erro. Podem ajudar por gentileza? Segue o código das classes.
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class br.com.alura.adopet.api.model.Pet.
If you're not sure why you're getting this error, please open an issue on GitHub.
Underlying exception : org.mockito.exceptions.base.MockitoException: Could not modify all classes [class java.lang.Object, class br.com.alura.adopet.api.model.Pet]
package br.com.alura.adopet.api.service;
import br.com.alura.adopet.api.dto.SolicitacaoAdocaoDto;
import br.com.alura.adopet.api.model.Abrigo;
import br.com.alura.adopet.api.model.Adocao;
import br.com.alura.adopet.api.model.Pet;
import br.com.alura.adopet.api.model.Tutor;
import br.com.alura.adopet.api.repository.AdocaoRepository;
import br.com.alura.adopet.api.repository.PetRepository;
import br.com.alura.adopet.api.repository.TutorRepository;
import br.com.alura.adopet.api.validacoes.ValidacaoSolicitacaoAdocao;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
@ExtendWith(MockitoExtension.class)
class AdocaoServiceTest {
@InjectMocks
private AdocaoService service;
@Mock
private AdocaoRepository adocaoRepository;
@Mock
private PetRepository petRepository;
@Mock
private TutorRepository tutorRepository;
@Mock
private EmailService emailService;
@Mock
private List<ValidacaoSolicitacaoAdocao> validacoes;
@Mock
private Pet pet;
@Mock
private Tutor tutor;
@Mock
private Abrigo abrigo;
private SolicitacaoAdocaoDto dto;
@Captor
private ArgumentCaptor<Adocao> adocaoCaptor;
@Test
void deveriaSalvarAdocaoAoSolicitar(){
//ARRANGE
this.dto = new SolicitacaoAdocaoDto(10l, 20l, "motivo qualquer");
given(petRepository.getReferenceById(dto.idPet())).willReturn(pet);
given(tutorRepository.getReferenceById(dto.idTutor())).willReturn(tutor);
given(pet.getAbrigo()).willReturn(abrigo);
//ACT
service.solicitar(dto);
//ASSERT
then(adocaoRepository).should().save(adocaoCaptor.capture());
Adocao adocaoSalva = adocaoCaptor.getValue();
assertEquals(pet, adocaoSalva.getPet());
assertEquals(tutor, adocaoSalva.getTutor());
assertEquals(dto.motivo(), adocaoSalva.getMotivo());
}
}
package br.com.alura.adopet.api.validacoes;
import br.com.alura.adopet.api.dto.SolicitacaoAdocaoDto;
import br.com.alura.adopet.api.exception.ValidacaoException;
import br.com.alura.adopet.api.model.Pet;
import br.com.alura.adopet.api.repository.PetRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.BDDMockito;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class ValidacaoPetDisponivelTest {
@InjectMocks
private ValidacaoPetDisponivel validacao;
@Mock
private PetRepository petRepository;
@Mock
private Pet pet;
@Mock
private SolicitacaoAdocaoDto dto;
@Test
void deveriaPermitirSolicitacaoDeAdocaoPet(){
//ARRANGE
BDDMockito.given(petRepository.getReferenceById(dto.idPet())).willReturn(pet);
BDDMockito.given(pet.getAdotado()).willReturn(false);
//ASSERT + ACT
Assertions.assertDoesNotThrow(() -> validacao.validar(dto));
}
@Test
void naoDeveriaPermitirSolicitacaoDeAdocaoPet(){
//ARRANGE
BDDMockito.given(petRepository.getReferenceById(dto.idPet())).willReturn(pet);
BDDMockito.given(pet.getAdotado()).willReturn(true);
//ASSERT + ACT
Assertions.assertThrows(ValidacaoException.class,() -> validacao.validar(dto));
}
}