Spring boot test code example - eunja511005/Tutorial GitHub Wiki

์ฐธ๊ณ  ์‹ธ์ดํŠธ

https://spring.io/guides/gs/testing-web/

ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ์ƒ์„ฑ

image

image

image


ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ

@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());
	}

}

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ - MockMvc ์žฌ์ •์˜

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);
		
	}
}

โš ๏ธ **GitHub.com Fallback** โš ๏ธ