๐งจ๋กฌ๋ณต
๋กฌ๋ณต: ์๋ฐ ๊ฐ๋ฐ ์ ์์ฃผ ์ฌ์ฉํ๋ ์ฝ๋ Getter, Setter, ๊ธฐ๋ณธ ์์ฑ์, toString ๋ฑ์ ์ด๋ ธํ ์ด์ ์ผ๋ก ์๋ ์์ฑํด์ฃผ๋ ์๋ฐ ๊ฐ๋ฐ์๋ค์ ํ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
build.gradle์ ๋ฏธ๋ฆฌ ๋ฃ์ด๋ compile('org.projectlombok:lombok')
๋ก ์ํ๋จ.
๐งจhello controller ์ฝ๋๋ฅผ ๋กฌ๋ณต์ผ๋ก ์ ํ
๊ธฐ์กด ์ฝ๋๋ฅผ ๋กฌ๋ณต์ผ๋ก ๋ฆฌํฉํ ๋งํ๋ ๊ณผ์ !
1. Dto ์์ฑ
๐ฝmain/ํจํค์ง/web/dto/HelloResponseDto
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public class HelloResponseDto {
private final String name;
private final int amount;
}
@Getter
: ์ ์ธ๋ ๋ชจ๋ ํ๋์ get ๋ฉ์๋ ์์ฑ
@RequiredArgsConstructor
์ ์ธ๋ ๋ชจ๋ final ํ๋๊ฐ ํฌํจ๋ ์์ฑ์๋ฅผ ์์ฑ
final์ด ์๋ ํ๋๋ ์์ฑ์์ ๋ฏธํฌํจ
2. Dto ํ
์คํธ ์ฝ๋
๐ฝtest/ํจํค์ง/dto/HelloResponseDtoTest
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class HelloResponseDtoTest {
@Test
public void ๋กฌ๋ณต_๊ธฐ๋ฅ_ํ
์คํธ() {
//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
assertj๋ผ๋ ํ ์คํธ ๊ฒ์ฆ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ฒ์ฆ ๋ฉ์๋
๊ฒ์ฆ ์ํ๋ ๋์์ ๋ฉ์๋ ์ธ์๋ก ๋ฐ์
isEqualTo
assertj์ ๋๋ฑ ๋น๊ต ๋ฉ์๋
assertThat์ ์๋ ๊ฐ๊ณผ isEqualTo์ ๊ฐ์ด ๊ฐ์ ๋๋ง ์ฑ๊ณต
์คํํ๋ฉด ์ ์์ ์ผ๋ก ๊ธฐ๋ฅ ์ํ (๋กฌ๋ณต์ @Getter๋ก get ๋ฉ์๋, @RequiredArgsConstructor๋ก ์์ฑ์๊ฐ ์๋์ผ๋ก ์์ฑ) ํ์ธ ๊ฐ๋ฅ
3. HelloController์๋ ์๋ก ๋ง๋ ResponseDto ์ฌ์ฉ
๐ฝmain/ํจํค์ง/web/HelloController
import com.jojoldu.book.springboot.web.dto.HelloResponseDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
//์ฌ๊ธฐ๊น์ง ๊ธฐ์กด ์ฝ๋----------------------------------๋ค์๋ถํฐ ์ถ๊ฐ
@GetMapping("/hello/dto")
//์ธ๋ถ์์ name (@RequestParam("name"))์ด๋ ์ด๋ฆ์ผ๋ก ๋๊ธด ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฉ์๋ ํ๋ผ๋ฏธํฐ name(String name)์ ์ ์ฅ
public HelloResponseDto helloDto(@RequestParam("name") String name,
@RequestParam("amount") int amount) {
return new HelloResponseDto(name, amount);
}
}
@RequestParam
์ธ๋ถ์์ API๋ก ๋๊ธด ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ์ด๋ ธํ ์ด์
์ถ๊ฐ๋ API ํ ์คํธํ๋ ์ฝ๋ ์ถ๊ฐ
๐ฝtest/ํจํค์ง/dto/HelloResponseDtoTest
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.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class)
@WebMvcTest
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void hello๊ฐ_๋ฆฌํด๋๋ค() throws Exception {
String hello = "hello";
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string(hello));
}
//์ฌ๊ธฐ๊น์ง ๊ธฐ์กด ์ฝ๋----------------------------------๋ค์๋ถํฐ ์ถ๊ฐ
@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 ์๋ต๊ฐ์ ํ๋๋ณ๋ก ๊ฒ์ฆ ๊ฐ๋ฅํ ๋ฉ์๋.
$ ๊ธฐ์ค์ผ๋ก ํ๋๋ช ๋ช ์
JSON์ด ๋ฆฌํด๋๋ API๋ ์ ์์ ์ผ๋ก ํ ์คํธ ํต๊ณผํ๋ ๊ฑฐ ํ์ธ ๊ฐ๋ฅ
'WEB > SPRING' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SPRING] Controller ํ ์คํธ ์ฝ๋ ์์ฑ (0) | 2021.10.06 |
---|---|
TDD์ ๋จ์ ํ ์คํธ, ํ ์คํธ ์ฝ๋ (0) | 2021.10.05 |
[SPRING] ์ธํ ๋ฆฌ์ ์ด๋ก ์คํ๋ง ๋ถํธ ์์ํ๊ธฐ (0) | 2021.10.04 |
MVC ์น ํ๋ ์์ํฌ์ DJANGO MTV (0) | 2021.10.03 |
REST API์ CRUD (0) | 2021.10.03 |
๋๊ธ