I'm trying to mock Service, which is responsible for getting Entities from Repository and Mapping them to Pojo. I'm getting an error and I don't understand why it works this way. Does someone know what I'm doing wrong?
Error:
class com.example.demo.businessLogic.person.Person cannot be cast to class
com.example.demo.postgres.entity.PersonEntity (com.example.demo.businessLogic.person.Person and
com.example.demo.postgres.entity.PersonEntity are in unnamed module of loader 'app')
java.lang.ClassCastException: class com.example.demo.businessLogic.person.Person cannot be cast to
class com.example.demo.postgres.entity.PersonEntity (com.example.demo.businessLogic.person.Person
and com.example.demo.postgres.entity.PersonEntity are in unnamed module of loader 'app')
personService.getAllPerson() returns Pojo:
@Override
public List<Person> getAllPerson() {
return personRepoPostgres.findAll().stream()
.map(personMapper::entityToPerson)
.collect(Collectors.toList());
}
Here is Test class:
@ExtendWith(MockitoExtension.class)
@ActiveProfiles("dev")
public class cTest {
@Mock
PersonRepoPostgres personRepoPostgres;
@Mock
PersonMapper personMapper;
@InjectMocks
PersonService personService;
@Test
void test(){
Mockito.when(personService.getAllPerson()).thenReturn(List.of(new Person("Zamor")));
List<Person> personArrayList = personService.getAllPerson();
Assertions.assertEquals(personArrayList.get(0), "Zamor");
}