๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
WEB/SPRING

[SPRING] ๊ธฐ์กด Controller ์ฝ”๋“œ๋ฅผ ๋กฌ๋ณต์œผ๋กœ ์ „ํ™˜

by ๋ญ‰๋ง๋ญ‰ 2021. 10. 7.

๐Ÿงจ๋กฌ๋ณต

๋กฌ๋ณต: ์ž๋ฐ” ๊ฐœ๋ฐœ ์‹œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ 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๋„ ์ •์ƒ์ ์œผ๋กœ ํ…Œ์ŠคํŠธ ํ†ต๊ณผํ•˜๋Š” ๊ฑฐ ํ™•์ธ ๊ฐ€๋Šฅ

๋Œ“๊ธ€