https://spring.io/guides/gs/testing-web/
@WebMvcTest : ํ
์คํธ์์ Spring Boot๋ ์ ์ฒด ์ปจํ
์คํธ๊ฐ ์๋ ์น ๋ ์ด์ด ๋ง ์ธ์คํด์คํ.
์ฌ๋ฌ ์ปจํธ๋กค๋ฌ๊ฐ ์๋ ์์ฉ ํ๋ก๊ทธ๋จ์์๋ ํ๋์ ์ปจํธ๋กค๋ฌ๋ง ์ธ์คํด์คํํ๋๋ก ์์ฒญํ ์๋ ์์ต๋๋ค.
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import com.eun.tutorial.dto.Book;
import com.eun.tutorial.service.BookService;
@WebMvcTest(MyWebMvcController.class)
class MyWebMvcControllerJTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private BookService service;
@Test
@DisplayName("viewBooks ํธ์ถ์ view-books ํ์ด์ง๋ฅผ ๋ฆฌํดํ๊ณ ๋ฐ์ดํฐ๋ ๋ชจํน๋ ๋ฐ์ดํฐ๊ฐ ๋ง๋์ง ํ์ธ")
public void getBooks() throws Exception {
List<Book> bookList = new ArrayList<Book>();
Book book1 = Book.builder()
.isbn("isbn1")
.name("name1")
.author("author1")
.build();
Book book2 = Book.builder()
.isbn("isbn2")
.name("name2")
.author("author2")
.build();
bookList.add(book1);
bookList.add(book2);
when(service.getBooks()).thenReturn(bookList);
MvcResult mvcResult = this.mockMvc.perform(get("/book/viewBooks").with(csrf()))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
assertEquals(bookList, (List<Book>) mvcResult.getModelAndView().getModelMap().get("books"));
assertEquals("view-books", mvcResult.getModelAndView().getViewName());
}
}
package com.eun.tutorial.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class MainControllerJTest {
@Autowired
MainController mainController;
@Autowired
MockMvc mockMvc;
@Test
public void contextLoads() throws Exception {
assertThat(mainController).isNotNull();
}
@Test
@DisplayName("MyWebMvcConfigurer.java ํ์ผ์์ CharacterEncodingFilter ์ค์ ํ HTTP Get ๋ฐฉ์์์ Request Param ์ ๋ฌ ์ ํ๊ธ ์ ๊นจ์ง")
public void hello() throws Exception {
String name = "ํ๊ธธ๋";
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/reqParam")
.param("name", name)
)
.andDo(print())
.andExpect(status().isOk())
.andReturn();
String responseBody = mvcResult.getResponse().getContentAsString();
assertEquals("Hello, "+name, responseBody);
}
@Test
@DisplayName("MyWebMvcConfigurer.java ํ์ผ์์ CharacterEncodingFilter ์ค์ ํ HTTP Post ๋ฐฉ์์์ Request Param ์ ๋ฌ ์ ํ๊ธ ์ ๊นจ์ง")
public void hello2() throws Exception {
String name = "ํ๊ธธ๋";
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/reqParam")
.param("name", name)
.contentType("text/plain;charset=UTF-8")
.with(csrf())
)
.andDo(print())
.andExpect(status().isOk())
.andReturn();
String responseBody = mvcResult.getResponse().getContentAsString();
assertEquals("Hello, "+name, responseBody);
}
@Test
@DisplayName("MockMvc ์์ฑ์ MessageConverters๋ฅผ ์ถ๊ฐํด์ค์ผ HTTP Get ๋ฐฉ์์์ Request Param ์ ๋ฌ ์ ํ๊ธ ์ ๊นจ์ง")
public void hello3() throws Exception {
String name = "ํ๊ธธ๋";
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(MainController.class)
// .addInterceptors(null)
.setMessageConverters(new StringHttpMessageConverter(StandardCharsets.UTF_8))
.build();
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/reqParam")
.param("name", name)
)
.andDo(print())
.andExpect(status().isOk())
.andReturn();
String responseBody = mvcResult.getResponse().getContentAsString();
assertEquals("Hello, "+name, responseBody);
}
}