Day 2
์ ๋ฒ ์๊ฐ์ ์ด์ด์ ํ ์คํธ ์ฝ๋๋ฅผ ์คํํด๋ณด๊ธฐ ์ํ ๊ณผ์
๐์ ๋ฒ์ ๋ง๋ค์๋ ํ
์คํธ๋ฅผ ์ํ ์ฝ๋๋ฅผ ๊ฒ์ฆ
ํ ์คํธ ์ฝ๋๋ฅผ ๋ง๋ค๊ธฐ ์ํด์๋
src/test/java ๋๋ ํ ๋ฆฌ๋ก ๊ฐ์
ํ ์คํธํ๋ ค๋ ์ฝ๋๊ฐ ๋ค์ด์๋ ํจํค์ง์ ๋๊ฐ์ ํจํค์ง๋ฅผ ์์ฑํ๊ณ
ํ ์คํธํ๋ ค๋ ํด๋์ค์ ์ด๋ฆ์ Test๋ฅผ ๋ถํ ์ด๋ฆ์ผ๋ก ํ ์คํธ์ฝ๋๋ฅผ ์์ฑํ๋ค
src/test/java/com/kyu/book/springboot/HelloControllerTest.class
package com.kyu.book.springboot;
import com.kyu.book.springboot.web.HelloController;
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.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = HelloController.class)
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void REHello() throws Exception{
String hello = "hello";
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string(hello));
}
}@RunWith(SpringRunner.class)
ํ ์คํธ๋ฅผ ์งํํ ๋ JUnit์ ๋ด์ฅ๋ ์คํ์ ์ธ์ ๋ค๋ฅธ ์คํ์๋ฅผ ์คํ
์ฌ๊ธฐ์๋ SpringRunner๋ผ๋ ์คํ๋ง ์คํ์๋ฅผ ์ฌ์ฉ
์คํ๋ง ๋ถํธ ํ ์คํธ์ JUnit ์ฌ์ด์ ์ฐ๊ฒฐ์ ์ญํ ์ ์ํ
@WebMvcTest
์ ์ธํ๊ฒ ๋๋ฉด โ @Controller, @ControllerAdvice ๋ฑ์ annotation์ ์ฌ์ฉํ ์ ์์
@Service, @Repository, @Component๋ฑ์ annotation์ ์ฌ์ฉํ ์ ์์
์ฌ๋ฌ ์คํ๋ง annotation์ค์์ Web์ ์ง์คํ ์ ์๋ annotation์ด๋ค
์ฌ๊ธฐ์์๋ ์ปจํธ๋กค๋ฌ๋ง ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์ ์ธํจ
@Autowired
์คํ๋ง์ด ๊ด๋ฆฌํ๋ Bean์ ์ฃผ์ ๋ฐ๋๋ค
Mockmvc
์น API๋ฅผ ํ ์คํธํ ๋ ์ฌ์ฉํจ
์คํ๋ง MVC ํ ์คํธ์ ์์์
์ด ํด๋์ค๋ฅผ ํตํด HTTP GET, POST ๋ฑ์ ๋ํ API ํ ์คํธ๋ฅผ ํ ์ ์์
perform(get("/hello"))
MockMvc๋ฅผ ํตํด /hello ์ฃผ์๋ก http get์์ฒญ
๋ฉ์๋ ์ฒด์ด๋์ด ๊ฐ๋ฅํด ์ฌ๋ฌ ๊ฒ์ฆ์ ์ด์ด์ ๊ฐ๋ฅ
andExpect(status().isOk())
perform์ ๊ฒฐ๊ณผ๊ฐ์ ๊ฒ์ฆ
http header์ status๋ฅผ ๊ฒ์ฆํ๋ค โ 200, 404, 500์ ๊ฐ์ด ์ํ ๊ฒ์ฆ
isOk()๋ ์ํ ์ฝ๋๊ฐ 200์ธ์ง ์๋์ง๋ฅผ ๊ฒ์ฆ
andExpect(content().string(hello))
mvc.perform์ ๊ฒฐ๊ณผ๋ฅผ ๊ฒ์ฆ
์๋ต ๋ณธ๋ฌธ์ ๋ด์ฉ์ ๊ฒ์ฆํ๋ค
Controller(ํ ์คํธํ๋ ์ฝ๋)์์ hello๋ฅผ ๋ฆฌํดํ๊ธฐ ๋๋ฌธ์ ์ด ๊ฐ์ด ๋ง๋์ง ํ์ธ
๐Lombok
gradle์ ์ถ๊ฐ
compile('org:projectlombock:lombok')
plugins์ ์ถ๊ฐ
๋กฌ๋ณต์ ํ๋ก์ ํธ๋ง๋ ์ค์ ์ ํด์ค์ผํจ โ build.gradle์ ์ถ๊ฐ / Settings > Build > Compiler > Annotation processors > Enable annotation processing
์์ ๋ฐฉ์์ผ๋ก gradle์ ์ถ๊ฐํ๋ ค๋๊น ์๋จ
๊ทธ๋์
build.gradle์ compileํ๋ dependencies์ ์๋์ ๊ฐ์ด ๋ฃ์ด์ฃผ๋๊น ์ ๋จ
๐HelloController์ฝ๋๋ฅผ ๋กฌ๋ณต์ผ๋ก ๋ณํํด๋ณด๊ธฐ
web์๋์ dtoํจํค์ง ์ถ๊ฐ
๋ชจ๋ ์๋ต dto๋ ์ด dtoํจํค์ง์ ์ถ๊ฐํ ์์
src/main/java/com/kyu/book/springboot/web/dto/HelloResponseDto.class
@RequestArgsConstructor
์ ์ธ๋ ๋ชจ๋ final ํ๋๊ฐ ํฌํจ๋ ์์ฑ์๋ฅผ ์์ฑํด์ค
final์ด ์๋ ํ๋๋ ์์ฑ์์ ํฌํจ๋์ง ์์
์์ ์ฝ๋์ ํ ์คํธ์ฝ๋
src/main/java/com/kyu/book/springboot/web/dto/HelloResponseDtoTest.class
*์ด ์ฝ๋๋ฅผ ์งํํ๋ ๊ณผ์ ์์ lombok์ @Getter๊ฐ ์ ์๋จน์์๋ค โ ๊ทธ๋์ plugin์ ๋ค์ด๊ฐ์ lombok์ disabled > apply ์ดํ, enabled > apply ํ๋๊น ์๋จ
*test ์งํ์ด ๋ถ๊ฐ๋ฅํ๋ค โ github์ ์ง๋ฌธํ์ด์ง๋ฅผ ๊ฐ์ ์ฐพ์๋ณด๋๊น gradle์ ๋ฒ์ ๋ฌธ์ ๊ฐ ์์๋ค.
์ฌ์ ํ ํ์ ์์๋ gradle4๋ฅผ ์ฌ์ฉํ๋ค๊ณ ํ๋๊น ๊ทธ๋ ค๋ฌ๋ํ๊ณ gradle4๋ฅผ ์ฌ์ฉํ์
๋ฒ์ ์ด ์ด๋ฏธ ์ฌ๋ผ๊ฐ์์๋ ๋ณ๊ฒฝํ๋๋ฐฉ๋ฒ์
terminal์ ์ด์ด์
๋ฅผ ์ ์ฉํด์ฃผ๋ฉด ์ ์์ ์ผ๋ก ํ ์คํธ๊ฐ ์งํ๋๋ค
assertThat
assertj๋ผ๋ ํ ์คํธ ๊ฒ์ฆ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ฒ์ฆ ๋ฉ์๋
๊ฒ์ฆํ๊ณ ์ถ์ ๋์์ ๋ฉ์๋ ์ธ์๋ฅผ ๋ฐ๋๋ค
๋ฉ์๋ ์ฒด์ด๋ ์ง์๋จ
isEqualTo()
๋๋ฑํ์ง ๋น๊ตํ๋ ๋ฉ์๋
๊ฐ์ด ๊ฐ์ผ๋ฉด ์ฑ๊ณต
assertj์ ์ฅ์ : ๋ฐฑ๊ธฐ์ ๋ ์ ํ๋ธ๋ฅผ ๋ณด๊ณ ๋ฐฐ์ฐ์
๐ResponseDto๋ฅผ ์ฌ์ฉํ๋๋ก ์ถ๊ฐ
src/main/java/com/kyu/boot/springboot/web/dto/HelloResponseDto.class์ ์ถ๊ฐ๋์ฝ๋
@RequestParam
์ธ๋ถ์์ API๋ก ๋๊ธด ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ ธ์ค๋ annotation์ ๋๋ค.
์ฌ๊ธฐ์์๋ ์ธ๋ถ์์ name(@RequestParam("name")) ์ด๋ ์ด๋ฆ์ผ๋ก ๋๊ธด ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฉ์๋ ํ๋ผ๋ฏธํฐ name(String name)์ ์ ์ฅํ๊ฒ ๋ฉ๋๋ค.
๐์ถ๊ฐ๋ API๋ฅผ ํ
์คํธ
src/test/java/com/kyu/book/springboot/web/HelloControllerTest.class์ ์ถ๊ฐ๋ ์ฝ๋
param
API ํ ์คํธํ ๋ ์ฌ์ฉ๋ ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ์ค์
๋จ, ๊ฐ์ String๋ง ํ์ฉ
๊ทธ๋์ ์ซ์/๋ ์ง ๋ฑ์ ๋ฐ์ดํฐ๋ ๋ฑ๋กํ ๋๋ ๋ฌธ์์ด๋ก ๋ณ๊ฒฝํด์ผ๋ง ๊ฐ๋ฅ
jsonPath
JSON ์๋ต๊ฐ์ ํ๋๋ณ๋ก ๊ฒ์ฆํ ์ ์๋ ๋ฉ์๋
$๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋๋ช ์ ๋ช ์
์ฌ๊ธฐ์ name, amount๋ฅผ ๊ฒ์ฆํ๋ $.name, $.amount๋ก ๊ฒ์ฆ
*์๊พธ ํ ์คํธ์ Test events were not received๊ฐ ๋ธ
โ Preference > Build, Execution, Deployment > Build Tools > Gradle๋ก ์ด๋ํด์
โRun test using์ Intellij IDEA๋ก ๋ณ๊ฒฝ
Last updated
Was this helpful?