模拟服务,注入存储库和映射器。 Spring 中的集成测试
Mocking Service, Injecting repository & mapper. Integration Tests in Spring
我正在尝试模拟服务,它负责从存储库中获取实体并将它们映射到 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() returns 波乔:
@Override
public List<Person> getAllPerson() {
return personRepoPostgres.findAll().stream()
.map(personMapper::entityToPerson)
.collect(Collectors.toList());
}
这是测试 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");
}
问题是您试图模拟被测方法,而您应该只模拟被测方法的依赖项。 Mockito.when
应该用于 PersonRepoPostgress
class 或 PersonMapper
class 中的方法,而不是 PersonService
.
PersonMapper
没有模拟实现,因此当调用 personMapper::entityToPerson
时,默认实现可能会尝试将 PersonEntity
转换为 Person
。
将您的模拟切换为类似这样的东西应该会有所帮助:
Mockito.when(personRepoPostgress.findAll()).thenReturn(List.of(new PersonEntity()));
Mockito.when(personMapper.entityToPerson(any(PersonEntity.class))).thenReturn(new Person("Zamor"));
我正在尝试模拟服务,它负责从存储库中获取实体并将它们映射到 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() returns 波乔:
@Override
public List<Person> getAllPerson() {
return personRepoPostgres.findAll().stream()
.map(personMapper::entityToPerson)
.collect(Collectors.toList());
}
这是测试 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");
}
问题是您试图模拟被测方法,而您应该只模拟被测方法的依赖项。 Mockito.when
应该用于 PersonRepoPostgress
class 或 PersonMapper
class 中的方法,而不是 PersonService
.
PersonMapper
没有模拟实现,因此当调用 personMapper::entityToPerson
时,默认实现可能会尝试将 PersonEntity
转换为 Person
。
将您的模拟切换为类似这样的东西应该会有所帮助:
Mockito.when(personRepoPostgress.findAll()).thenReturn(List.of(new PersonEntity()));
Mockito.when(personMapper.entityToPerson(any(PersonEntity.class))).thenReturn(new Person("Zamor"));