ํ๋ก์ ํธ ์์ฑ์ ์ด๊ณณ์ ์ฐธ์กฐํ๋ค.
TDD (Test-driven Development) ๋ ํ
์คํธ ์ฃผ๋ ๊ฐ๋ฐ๋ก์ ํ
์คํธ๊ฐ ์ฃผ๊ฐ ๋์ด ๊ฐ๋ฐํ๋ ๋ฐฉ๋ฒ๋ก ์ด๋ค.
TDD๋ฅผ ์ฌ์ฉํจ์ผ๋ก์จ ์ฐ๋ฆฌ๋
build.gradle
์ ๋ค์์ ์ถ๊ฐํ๋ค.
dependencies {
// RestAPI
implementation 'org.springframework.boot:spring-boot-starter-web'
// Test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
/web/controller
ํด๋๋ฅผ ๋ง๋ ๋ค.HelloController.java
๋ฅผ ๋ง๋ ๋ค.package com.rivernine.demo.web.controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
@RestController
@ResponseBody
๋ฅผ ๋ฉ์๋๋ง๋ค ์ ์ธํด ์ฃผ์๋ค.@GetMapping
@RequestMapping(method = RequestMethod.GET)
์ ์ฌ์ฉํ์test
์๋์ ๋์ผ ๊ตฌ์กฐ์ ํด๋๋ฅผ ๋ง๋ ๋ค. web/controller
HelloControllerTest.java
๋ฅผ ๋ง๋ ๋ค.package com.rivernine.demo.web.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
@ExtendWith(MockitoExtension.class)
@WebMvcTest
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void return_hello() throws Exception {
String hello = "hello";
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string(hello));
}
}
@ExtendWith
@WebMvcTest
@Controller
, @ControllerAdvice
๋ฅผ ์ฌ์ฉํ ์ ์๋ค.@Autowired
private MockMvc mvc
mvc.perform(get("/hello"))
.andExpect(status().isOk())
mvc.perform
์ ๊ฒฐ๊ณผ๋ฅผ ๊ฒ์ฆํ๋ค..andExpect(content().string(hello))
mvc.perform
์ ๊ฒฐ๊ณผ๋ฅผ ๊ฒ์ฆํ๋ค.๊ฐ method๋ณ๋ก ํ
์คํธ๋ฅผ ์คํํด ๋ณผ ์ ์๋ค.
ํ์๋ VSCode
๋ฅผ ์ฌ์ฉํ๊ณ ์๊ณ , Java Extension Pack
์ ์ถ๊ฐํ์ฌ ์ฌ์ฉํ๋ค.
1. Test ์คํ
Run Test
๋ฅผ ๋๋ฅด๋ฉด unit test๋ฅผ ์งํํ ์ ์๋ค.
2. ๊ฒฐ๊ณผ ํ์ธ
๋๋ฒํฌ ์ฝ์
์ ๋ณด์.
was๋ฅผ ํ์๋กํ๋ Controller Test์ด๊ธฐ์ Spring-boot application์ด ์คํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
Test ๊ฒฐ๊ณผ๋ VSCode ์ข์ธก ํ๋ผ์คํฌ ์์ด์ฝ์ ํด๋ฆญํ์ฌ ํ์ธํ ์ ์๋ค.
# ๋น๋
./gradlew build
# Spring-boot ์คํ
java -jar ./build/lib/*.jar
# ํ
์คํธ
curl localhost:8080/hello # hello
๋ชจ๋ ์์ค๋ ๊นํ๋ธ์ ์ฌ๋ ค๋์๋ค.
์ฐธ๊ณ ์์ : ์คํ๋ง๋ถํธ์ AWS๋ก ํผ์ ๊ตฌํํ๋ ์น ์๋น์ค