【发布时间】:2021-10-03 14:18:28
【问题描述】:
我正在尝试模拟服务,它负责从存储库获取实体并将它们映射到 Pojo。我收到一个错误,我不明白为什么它会这样工作。 有人知道我在做什么错吗?
错误:
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() 返回 Pojo:
@Override
public List<Person> getAllPerson() {
return personRepoPostgres.findAll().stream()
.map(personMapper::entityToPerson)
.collect(Collectors.toList());
}
这里是测试类:
@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");
}
【问题讨论】: