JSheep`s Album

[Spring Boot] 테스트 코드 작성하기 본문

1st Album - Dev/02. Spring

[Spring Boot] 테스트 코드 작성하기

JSheep 2022. 9. 13. 17:43

1. 테스트 코드 작성

- main  패키징 하위 

   - Application 생성

   - web 패키지 생성

test 패키징 하위

   - main 패키징 하위와 동일하게 생성

 

1. 1. HelloResponseDto

package com.jsheep.book.springboot.web.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/*
    선언된 모든 필드의 get 메소드를 생성해 줍니다
 */
@Getter
/*
    선언된 모든 final 필드가 포함된 생성자를 생성해 줍니다.
    final이 없는 필드는 생성자에 포함되지 않습니다.
 */
@RequiredArgsConstructor
public class HelloResponseDto {

    private final String name;
    private final int amount;
}

1. 2. HelloController

package com.jsheep.book.springboot.web;

import com.jsheep.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")
    public HelloResponseDto helloDto(@RequestParam("name") String name,
                                     @RequestParam("amount") int amount) {
        return new HelloResponseDto(name, amount);
    }
}

1. 3. HelloResponseDtoTest

package com.jsheep.book.springboot.web.dto;

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와 같이 메소드를 이어서 사용할 수 있습니다.
            isEqualTo
             - assertj의 동등 비교 메소드입니다.
             - assertThat에 있는 값과 isEqualTo의 값을 비교해서 같을 떄만 성공입니다.
         */
    }
}

1. 4. HelloControllerTest

package com.jsheep.book.springboot.web;

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.result.MockMvcResultMatchers.jsonPath;
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
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)));
    }

}

1. 5. HelloContoller 테스트 메소드 실행결과

Comments