0

When I try to test my CRUD operations in my Spring Boot Applications, I get NullPointerException saying that this.Repository is null. What can I do to resolve this issue? Am I missing something?

My test class:

@RunWith(MockitoJUnitRunner.class)
class AppointmentServiceTest {
 @Mock
 private AppointmentRepository appointmentRepository;
 @InjectMocks
 private AppointmentService appointmentService;
 @Test
 void shouldGetAllAppointments() {
 List<Appointment> appointments = new ArrayList<>();
 appointments.add(new Appointment());
 given(appointmentRepository.findAll()).willReturn(appointments);
 List<Appointment> expectedAppointments = appointmentService.getAllAppointments();
 assertEquals(expectedAppointments, appointments);
 verify(appointmentRepository.findAll());
 }
}

I am getting NullPointerException:

java.lang.NullPointerException: Cannot invoke "com.app.hospitalmanagementsystem.repository.AppointmentRepository.findAll()" because "this.appointmentRepository" is null
asked Aug 21, 2022 at 12:07
6
  • 1
    what are given and verify methods? To which library do they belong? Probably makes sense to add a corresponding tag Commented Aug 21, 2022 at 12:12
  • Hello, given is from org.mockito.BDDMockito and verify is from org.mockito.Mockito . Maybe the issue is caused by wrong static imports? Commented Aug 21, 2022 at 12:19
  • I haven't worked with BDDMockito. You can try Mockito.when(appointmentRepository.findAll()).thenReturn(appointments); Also, Are you sure you're running with JUnit 4 I mean is it your real intention? Or you need JUnit 5 really? Commented Aug 21, 2022 at 12:47
  • Hey, you are right. I was trying to run with JUnit4 while I needed JUnit5. I just replaced @RunWith() with @DataJpaTest and it works fine. Thank you. Commented Aug 21, 2022 at 13:05
  • @DataJpaTest is a compeletely different beast :) Its not even a unit test anymore, it runs Spring infrastructure with real repositories, so make sure you understand the difference. For plain Unit test powered by JUnit 5, use @ExtendsWIth(MockitoExtension.class) Commented Aug 21, 2022 at 13:38

1 Answer 1

2

Since the spring boot is tagged here, the chances that you're using spring boot 2.x (1.x is outdated these days)

But if so, you should be running JUnit 5 tests (spring boot 2.x works with Junit 5)

So instead of @RunWith annotation, use @ExtendsWith

Then place the breakpoint in the test and make sure that mockito extension has actually worked and the mock is created.

Now as for the given - I can't say for sure, I haven't used this syntax (BDD Mockito), but in a "clean mockito" it should be Mockito.when(..).thenReturn

All-in-all try this code:

@ExtendsWith(MockitoExtension.class)
class AppointmentServiceTest {
 @Mock
 private AppointmentRepository appointmentRepository;
 @InjectMocks
 private AppointmentService appointmentService;
 @Test
 void shouldGetAllAppointments() {
 List<Appointment> appointments = new ArrayList<>();
 appointments.add(new Appointment());
 Mockito.when(appointmentRepository.findAll()).thenReturn(appointments);
 List<Appointment> expectedAppointments = appointmentService.getAllAppointments();
 assertEquals(expectedAppointments, appointments);
 verify(appointmentRepository.findAll());
 }
}
answered Aug 21, 2022 at 13:54
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.