1 ๋ถ„ ์†Œ์š”

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์˜ ๋‹ค์Œ ์žฅ์ ๋“ค ๋•Œ๋ฌธ์ด๋‹ค.

  1. CoreMatchers์™€ ๋‹ฌ๋ฆฌ ์ถ”๊ฐ€์ ์œผ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.
    • Junit์˜ assertThat์„ ์‚ฌ์šฉํ•˜๋ฉด is()์™€ ๊ฐ™์ด CoreMatchers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
  2. ์ž๋™์™„์„ฑ์ด ์ข€ ๋” ํ™•์‹คํ•˜๊ฒŒ ์ง€์›๋œ๋‹ค.
    • 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๋กœ ๊ฒ€์ฆํ•œ๋‹ค.

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ