0

I'm trying test a RestAPI in Java, but I can't mock my Service class

When start test, it gives me an error: java.lang.NullPointerException: Cannot invoke "com.tcc.tccbackend.services.PacienteService.findById(java.lang.Integer)" because "this.service" is null.

Code:

@WebMvcTest(PacienteController.class)
public class PacienteTest extends BaseTest {

@Mock
    private PacienteService service;

@Autowired
    private MockMvc mockMvc;

@BeforeEach
    public void setup() {
        RestAssuredMockMvc.mockMvc(mockMvc);
    }

@Test
    @DisplayName("Retorna sucesso quando busca um paciente ")
    public void t4() {
        Mockito.when(service.findById(9999))
                .thenReturn(new Paciente(9999, "Gilberson", "[email protected]", "68211836104", "(67) 99625-5371", new Date(), List.of()));
        RestAssuredMockMvc
                .given()
                .header("Authorization", getJWT())
                .accept(ContentType.JSON)
                .when()
                .get("/pacientes/9999")
                .then().statusCode(200);
    }

BaseTest.class:

public class BaseTest {
    public BaseTest(){
        baseURI = "http://localhost";
        port = 8080;
        basePath = "/api";
    }

    public static String getJWT() {
        return given()
                .body("{\n" + "\t\"email\": \"[email protected]\",\n" + "\t\"senha\": \"senha\"\n" + "}")
                .contentType(ContentType.JSON)
                .when()
                .post("/user/login")
                .then()
                .extract()
                .path("token");
    }
}

Versions:

  • Java 17
  • RestAssured 5.3.0
  • SpringBoot 2.7.0
  • SpringBoot Starter Test 2.7.0
  • Junit 4.13.2
1
  • Which JUnit extension do you use? Can you show your class header?
    – knittl
    Commented Jan 20, 2023 at 8:40

2 Answers 2

0

Your mixing up unit test and integration test config.

Annotations for unit tests look like this:

@ExtendWith(MockitoExtension.class)
class MyTest {

    @Mock
    private ServiceA myMock;

    @Spy
    private ServiceB mySpy;

    @InjectMocks
    private ServiceC myClassUnderTest;

    @Test
    void receive() {
         // test logic
    }

}

Annotations for integration tests look like this:

@ActiveProfiles("test")
@SpringBootTest
class MyTest {

    @MockBean
    private ServiceA myMock;

    @SpyBean
    private ServiceB mySpy;

    @Autowired
    private ServiceC myClassUnderTest;

    @Test
    void receive() {
         // test logic
    }

}

I don't know about your test config because you didn't post it here, but I suppose you should use @MockBean instead of @Mock for PacienteService service.

0

Your PacieteService""" should be autowired not mocked. SO... @Mock private PacienteService service;

Should be *************

@Autowired private PacienteService service;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.