Home > Software engineering >  Spring Boot - unit test not detecting the controller test
Spring Boot - unit test not detecting the controller test

Time:10-05

I have written unit test for the controller class but when I run unit test using "mvn test" or directly from "SpringBootDemo1ApplicationTests" only test under "SpringBootDemo1ApplicationTests" class runs, and it doesn't pick up test from "BookControllerTest" class.

SpringBootDemo1ApplicationTests.java:

package com.sprboot.SpringBootDemo1;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootDemo1ApplicationTests {

    @Test
    void contextLoads() {
    }

}

BookControllerTest.java

package com.sprboot.SpringBootDemo1;

import com.sprboot.SpringBootDemo1.controllers.BookController;
import com.sprboot.SpringBootDemo1.models.Book;
import com.sprboot.SpringBootDemo1.services.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Optional;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

@AutoConfigureJsonTesters
@WebMvcTest(BookController.class)
@SpringBootTest
public class BookControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private BookService bookService;

    @Autowired
    private JacksonTester<Book> jsonBook;

    @Test
    private void canRetriveBookById() throws Exception {
        given(bookService.getBookByID(123)).willReturn(Optional.of(new Book(123, "test book")));

        MockHttpServletResponse response = mockMvc.perform(
                        get("/getBookById/123")
                                .accept(MediaType.APPLICATION_JSON))
                .andReturn().getResponse();

        // then
        assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
        assertThat(response.getContentAsString()).isEqualTo(
                jsonBook.write(new Book(1, "test book")).getJson()
        );

    }

    private void canRetriveBookByIdV2() throws Exception {
        given(bookService.getBookByID(123)).willReturn(Optional.of(new Book(123, "test book")));

        MockHttpServletResponse response = mockMvc.perform(
                        get("/getBookById/123")
                                .accept(MediaType.APPLICATION_JSON))
                .andReturn().getResponse();

        // then
        assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
        assertThat(response.getContentAsString()).isEqualTo(
                jsonBook.write(new Book(1, "test book")).getJson()
        );

    }
}

I was expecting test "canRetriveBookById" to run as well but only test "contextLoads" runs, not sure whats wrong here.

CodePudding user response:

It's because your @Test method is private.

From the @Test annotations documentation:

@Test methods must not be private or static and must not return a value.
  • Related