โ๏ธ TDD
ํ
์คํธ ์ฃผ๋ ๊ฐ๋ฐ(Test Driven Development)
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = HomeController.class)
public class HomeControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void home_return() throws Exception {
//when
String home = "home";
//then
mvc.perform(get("/home"))
.andExpect(status().isOk())
.andExpect(content().string(home));
}
}
โ๏ธ @RunWith(SpringRunner.class)
ํ
์คํธ๋ฅผ ์งํํ ๋ JUnit์ ๋ด์ฅ๋ ์คํ์ ์ธ์ ๋ค๋ฅธ ์คํ์ ์คํ
์คํ๋ง ๋ถํธ ํ
์คํธ์ JUnit ์ฌ์ด์ ์ฐ๊ฒฐ์ ์ญํ
โ๏ธ @WebMvcTest
Web(Spring MVC)์ ์ง์คํ ์ ์๋ ์ด๋
ธํ
์ด์
@Controller, @ControllerAdvice ๋ฑ ์ฌ์ฉ๊ฐ๋ฅ
@Service, @Compnent, @Repository ์ฌ์ฉ๋ถ๊ฐ
โ๏ธ @AutoWired
์คํ๋ง์ด ๊ด๋ฆฌํ๋ Bean ์ฃผ์
์์ผ์ค
โ๏ธ MockMvc
์น API ํ
์คํธํ ๋ ์ฌ์ฉ
์ด๋ฅผ ํตํด HTTP GET, POST, DELETE ๋ฑ์ ๋ํ API ํ
์คํธ ๊ฐ๋ฅ
โ๏ธ mvc.perform(get("/home"))
/home ์ฃผ์๋ก HTTP GET ์์ฒญ
โ๏ธ .andExpect(status().isOK())
๊ฒฐ๊ณผ๋ฅผ ๊ฒ์ฆํ๋ andExpect๋ก ์ฌ๋ฌ๊ฐ ๋ถ์ฌ์ ์ฌ์ฉ ๊ฐ๋ฅ
status()๋ HTTP Header๋ฅผ ๊ฒ์ฆํ๋ ๊ฒ์ผ๋ก ๊ฒฐ๊ณผ์ ๋ํ HTTP Status ์ํ ํ์ธ ๊ฐ๋ฅ
isOK()๋ 200 ์ฝ๋๊ฐ ๋ง๋์ง ํ์ธํ๊ณ ์์
โ๏ธ .andExpect(content().string(hello))
mvc.perform์ ๊ฒฐ๊ณผ ๊ฒ์ฆ
์๋ต ๋ณธ๋ฌธ์ ๋ด์ฉ ๊ฒ์ฆ