简体   繁体   中英

Spring Boot - unit test not detecting the controller test

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM