英文:
Spring Boot Jpastreamer - java.lang.NullPointerException in JUnit Test
问题
这个问题可能出在Mockito.when(personStreamMock.filter(Mockito.any())).thenReturn(filteredStreamMock);
这一行上,它抛出了空指针异常。
为了解决这个问题,你可以尝试将personStreamMock
模拟对象的filter
方法与filteredStreamMock
进行交互时的行为进行模拟。这可以通过使用Mockito.doReturn()
来完成,而不是Mockito.when()
。
请尝试将以下代码行替换原来的代码:
Mockito.doReturn(filteredStreamMock).when(personStreamMock).filter(Mockito.any());
这将设置personStreamMock
对象的filter
方法在调用时返回filteredStreamMock
,而不会引发空指针异常。
希望这可以解决你的问题。如果还有其他问题,请随时提出。
英文:
I have a problem to write JUnit test for Jpastreamer in MY Spring Boot app.
Here is the service shown below
@Service
@RequiredArgsConstructor
public class PersonService {
private final JPAStreamer jpaStreamer;
public List<Person> findByAgeForJpaStreamer(Integer age) {
return jpaStreamer.stream(Person.class).
filter(Person$.age.equal(age)).
collect(Collectors.toList());
}
}
Here is the BaseServiceTest shown below
@ExtendWith(MockitoExtension.class)
@ActiveProfiles(value = "test")
public abstract class BaseServiceTest {
}
Here is PersonServiceTest shown below
class PersonServiceTest extends BaseServiceTest {
@InjectMocks
private PersonService personService;
@Mock
private JPAStreamer jpaStreamer;
private List<Person> personList;
@BeforeEach
public void setup() {
personList = Arrays.asList(
Person.builder().id(1L).firstName("John").lastName("Doe").age(33).birthday(LocalDate.of(1990, 5, 15)).build(),
Person.builder().id(2L).firstName("Jane").lastName("Smith").age(27).birthday(LocalDate.of(1996, 9, 20)).build(),
Person.builder().id(3L).firstName("David").lastName("Johnson").age(32).birthday(LocalDate.of(1991, 9, 20)).build()
);
}
@Test
public void testJpaStreamerFindByAge() {
// given
Stream<Person> personStreamMock = personList.stream();
Stream<Person> filteredStreamMock = personStreamMock
.filter(person -> person.getAge() == 32);
List<Person> collectedListMock = filteredStreamMock.collect(Collectors.toList());
// whwn
Mockito.when(jpaStreamer.stream(Person.class)).thenReturn(personStreamMock);
Mockito.when(personStreamMock.filter(Mockito.any())).thenReturn(filteredStreamMock);
Mockito.when(filteredStreamMock.collect(Collectors.toList())).thenReturn(collectedListMock);
// then
List<Person> actualPersons = personService.findByAgeForJpaStreamer(32);
// Assert the result
assertEquals(collectedListMock, actualPersons);
assertEquals(collectedListMock.size(), actualPersons.size());
// Assert each person's properties
for (int i = 0; i < collectedListMock.size(); i++) {
assertEquals(collectedListMock.get(i).getId(), actualPersons.get(i).getId());
assertEquals(collectedListMock.get(i).getFirstName(), actualPersons.get(i).getFirstName());
assertEquals(collectedListMock.get(i).getLastName(), actualPersons.get(i).getLastName());
assertEquals(collectedListMock.get(i).getAge(), actualPersons.get(i).getAge());
assertEquals(collectedListMock.get(i).getBirthday(), actualPersons.get(i).getBirthday());
}
// Verify the JPAStreamer method calls
Mockito.verify(jpaStreamer, Mockito.times(1)).stream(Person.class);
Mockito.verify(personStreamMock, Mockito.times(1)).filter(Mockito.any());
Mockito.verify(filteredStreamMock, Mockito.times(1)).collect(Collectors.toList());
}
I have a problem in that line
Mockito.when(personStreamMock.filter(Mockito.any())).thenReturn(filteredStreamMock);
It throws null pointer exception.
How can I fix it?
答案1
得分: 0
在项目中定义了jpastreamer配置后,问题消失了。
@Configuration
@RequiredArgsConstructor
public class JPAStreamerConfig {
private final EntityManagerFactory entityManagerFactory;
@Bean
public JPAStreamer jpaStreamer() {
return JPAStreamer.of(entityManagerFactory);
}
}
英文:
After defining jpastreamer configuration in the project, the issue disappeared.
@Configuration
@RequiredArgsConstructor
public class JPAStreamerConfig {
private final EntityManagerFactory entityManagerFactory;
@Bean
public JPAStreamer jpaStreamer() {
return JPAStreamer.of(entityManagerFactory);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论