๐[์คํ๋ง๋ถํธ์ AWS๋ก ํผ์ ๊ตฌํํ๋ ์น ์๋น์ค] Test ํด๋์ค์ ์ฌ์ฉ๋๋ ์ ๋ ธํ ์ด์ ๋ฐ ์ฝ๋ ์ค๋ช 2
Test Class 2
package com.jojoldu.book.springboot.web.dto;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class HelloResponseDtoTest {
@Test
public void lombok_function_test() {
// given
String name = "test";
int amount = 1000;
// when
HelloResponseDto dto = new HelloResponseDto(name, amount);
// then
assertThat(dto.getName()).isEqualTo(name);
assertThat(dto.getAmount()).isEqualTo(amount);
}
}
assertThat
assertThat์ assertj๋ผ๋ ํ
ํธ ๊ฒ์ฆ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ฒ์ฆ ๋ฉ์๋์ด๋ค.
๊ฒ์ฆํ๊ณ ์ถ์ ๋์์ ๋ฉ์๋ ์ธ์๋ก ๋ฐ๊ณ , ๋ฉ์๋ ์ฒด์ด๋์ด ์ง์๋์ด isEqualTo()์ ๊ฐ์ ๋ฉ์๋๋ฅผ ์ด์ด ์ฌ์ฉํ ์ ์๋ค.
์ Junit์ assertThat์ด ์๋ assertj์ assertThat์ ์ฌ์ฉํ์๊น?
assertj์ ๋ค์ ์ฅ์ ๋ค ๋๋ฌธ์ด๋ค.
- CoreMatchers์ ๋ฌ๋ฆฌ ์ถ๊ฐ์ ์ผ๋ก ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํ์ํ์ง ์๋ค.
- Junit์ assertThat์ ์ฌ์ฉํ๋ฉด is()์ ๊ฐ์ด CoreMatchers ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํ์ํ๋ค.
- ์๋์์ฑ์ด ์ข ๋ ํ์คํ๊ฒ ์ง์๋๋ค.
- IDE์์๋ CoreMatchers์ ๊ฐ์ Matcher ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์๋์์ฑ ์ง์์ด ์ฝํ๋ค.
isEqualTo()
assertj์ ๋๋ฑ ๋น๊ต ๋ฉ์๋์ด๋ค.
assertThat์ ์๋ ๊ฐ๊ณผ isEqualTo์ ๊ฐ์ ๋น๊ตํด ๊ฐ์ ๋๋ง ์ฑ๊ณตํ๋ ์ญํ ์ด๋ค.
org.junit.Test์ org.junit.jupiter.api.Test
org.junit.Test์ org.junit.jupiter.api.Test๋ ๋ชจ๋ JUnit ํ๋ ์์ํฌ์์ ํ
์คํธ ๋ฉ์๋๋ฅผ ์ง์ ํ๊ธฐ ์ํ ์ ๋
ธํ
์ด์
์ด๋ค.
ํ์ง๋ง, org.junit.jupiter.api.Test๋ org.junit.Test์ ๋ค๋ฅด๊ฒ Junit5์์ ๋์
๋ ์ ๋
ธํ
์ด์
์ด๋ค.
Junit5๋ Java8 ์ด์์ ์ง์ํ๋ฉฐ, ํฅ์๋ ๊ธฐ๋ฅ๊ณผ ์ ์ฐ์ฑ์ ์ ๊ณตํ๋ค.
๊ฒฐ๊ตญ Junit5 ์์๋ org.junit.jupiter.api.Test๋ฅผ ์ฌ์ฉํด์ผํ๋ค.
Test Class 3
[HelloControllerTest]
import static org.hamcrest.Matchers.is;
import org.junit.jupiter.api.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.client.match.MockRestRequestMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@Test
public void helloDto๊ฐ_๋ฆฌํด๋๋ค() throws Exception {
String name = "hello";
int amount = 1000;
mvc.perform(
get("/hello/dto").param("name", name)
.param("amount", String.valueOf(amount)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is(name)))
.andExpect(jsonPath("$.amount", is(amount)));
}
param
API ํ
์คํธ๋ฅผ ํ ๋ ์ฌ์ฉ๋ ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ์ค์ ํ๋ค.
๋จ, ๊ฐ์ String๋ง ํ์ฉ๋๋ฉฐ, ์ซ์/๋ ์ง์ ๊ฐ์ ๋ฐ์ดํฐ๋ ๋ฑ๋กํ ๋๋ ๋ฌธ์์ด๋ก ๋ณ๊ฒฝํด์ผํ๋ค.
jsonPath()
JSON ์๋ต๊ฐ์ ํ๋๋ณ๋ก ๊ฒ์ฆํ ์ ์๋ ๋ฉ์๋๋ก, $๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋๋ช
์ ๋ช
์ํ๋ค.
์ฌ๊ธฐ์๋ name, amount๋ฅผ ๊ฒ์ฆํ๋ฏ๋ก, $.name, $.amount๋ก ๊ฒ์ฆํ๋ค.
๋๊ธ๋จ๊ธฐ๊ธฐ