[Spring Boot] ํ†ตํ•ฉ Test

2021. 8. 17. 08:00ใ†Back-End ์ž‘์—…์‹ค/Spring Framework

728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”? ์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ ํ†ตํ•ฉ Test ์ž‘์„ฑ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€ ํ•ด ๋ณด๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

๋ฐ”๋กœ ์‹œ์ž‘ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 

์ฝ”๋“œ์— ๊ด€๋ จํ•œ ๋‚ด์šฉ์€ ์ฃผ๋‹ˆํ•˜๋ž‘์˜ Github์—์„œ ํ™•์ธ ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!




 

 

๐Ÿ“‹ ๋ชฉ์ฐจ


01.[Spring Boot] thymeleaf์™€ Spring Boot
02.[Spring Boot] Spring Web ๊ฐœ๋ฐœ ๊ธฐ์ดˆ
03.[Spring Boot] ํšŒ์› ๊ด€๋ฆฌ ์˜ˆ์ œ - Backend
04.[Spring Boot] Service ๊ฐœ๋ฐœ ๋ฐ Test Case ์ž‘์„ฑ
05.[Spring Boot] Spring Bean๊ณผ ์˜์กด๊ด€๊ณ„
06.[Spring Boot] Java Code๋กœ ์ง์ ‘ Spring Bean ๋“ฑ๋ก
07.[Spring Boot] ๋“ฑ๋ก, ๋ชฉ๋ก ๋ณด๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ
08.[Spring Boot] ๊ธฐ์กด ์ฝ”๋“œ ์† ์•ˆ๋Œ€๊ณ , ์„ค์ •์œผ๋กœ ๊ตฌํ˜„ Class ๋ณ€๊ฒฝ
09.[Spring Boot] ํ†ตํ•ฉ Test
10.[Spring Boot] JPA
11.[Spring Boot] Spring Data JPA
12.[Spring Boot] AOP

 

 


 

 

๐Ÿ“Œ Integration Test


ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋Š” ๋‹จ์œ„ ํ…Œ์ŠคํŠธ์™€ ๊ฐ™์ด ๊ธฐ๋Šฅ ๊ฒ€์ฆ์ด ์•„๋‹Œ, Spring์—์„œ ์‹ค์ œ ์šด์˜ ํ™˜๊ฒฝ๊ณผ ๊ฐ™์ด ์ „์ฒด Flow๊ฐ€ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” Test์ธ ๊ฒƒ์ด์—์š”.

์—ฌ๊ธฐ์„œ @SpringBootTest๊ฐ€ ์‚ฌ์šฉ์ด ๋˜๋Š”๋ฐ, ์ด๊ฒƒ์€ @SpringBootApplication์„ ์ฐพ์•„๊ฐ€์„œ ๋ชจ๋“  Bean์„ Scanํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋ชจ๋“  Bean์„ Loadํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด๊ฒ๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๋‹จ์ ์ด ์žˆ๋‹ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , Test ๋‹จ์œ„๊ฐ€ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๋””๋ฒ„๊น…์ด ์–ด๋ ค์šด ํŽธ์ด์—์š”

์‚ฌ์‹ค ํ†ตํ•ฉ Test๋ณด๋‹ค๋Š” ๋‹จ์œ„ Test์— ์ง‘์ค‘ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€ ๊ฒƒ์ด์—์š”. ์–ด์ฉ” ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋‹จ์œ„ Test Code ์ž‘์„ฑ ๊ณต๋ถ€๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

 

 

MemberServiceIntegrationTest.java

package hello.hellospring.service;

import hello.hellospring.domain.Member;
import hello.hellospring.repository.MemberRepository;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import static org.junit.jupiter.api.Assertions.assertThrows;

@SpringBootTest
// DB์— Data๋ฅผ ์ €์žฅํ•œ ๋’ค Spring์ด Test๋ฅผ ํ•œ ๋’ค์— DATA๋ฅผ ์ง€์›Œ(RollBack)์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
@Transactional      
class MemberServiceIntegrationTest {

    @Autowired MemberService memberService;
    @Autowired MemberRepository memberRepository;

    @Test
    // @commit   ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ฉด DB์—์„œ RollBack์ด ์•„๋‹Œ commit์„ ํ•ด ๋ฒ„๋ ค์„œ Data๋ฅผ ์ž…๋ ฅํ•ด ๋ฒ„๋ฆฐ๋‹ค.
    void ํšŒ์›๊ฐ€์ž…() {

        // given(๋ฌด์—‡์ธ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ)
        Member member = new Member();
        member.setName("JunyHarang");

        // when(์ด๊ฒƒ์„ ์‹คํ–‰ ํ–ˆ์„ ๋•Œ)
        Long saveId = memberService.join(member);

        // then(๊ฒฐ๊ณผ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์™€์•ผ ํ•œ๋‹ค. (๊ฒ€์ฆ๋ถ€))
        Member findMember = memberService.findOne(saveId).get();
        Assertions.assertThat(member.getName()).isEqualTo(findMember.getName());

    } // ํšŒ์›๊ฐ€์ž… () ๋

    @Test
    public void ์ค‘๋ณตํšŒ์›_์˜ˆ์™ธ() {
        // given
        Member member1 = new Member();
        member1.setName("JunyHarang");

        Member member2 = new Member();
        member2.setName("JunyHarang");

        // when
        memberService.join(member1);

        IllegalStateException e = assertThrows(IllegalStateException.class, () -> memberService.join(member2));

        Assertions.assertThat(e.getMessage()).isEqualTo("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํšŒ์› ์ž…๋‹ˆ๋‹ค.");

        //then
    }

    @Test
    void findMembers() {
    } // findMembers() ๋

    @Test
    void findOne() {
    } // findOne() ๋
} // Class ๋
  • @SpringBootTest : Spring Container์™€ ํ•จ๊ป˜ Test๋ฅผ ์‹คํ–‰
                                      Spring Boot๋Š” ํ•ด๋‹น Annotation์„ ํ†ตํ•ด Application Test์— ํ•„์š”ํ•œ ๊ฑฐ์˜ ๋ชจ๋“  ์˜์กด์„ฑ ์ œ๊ณต
                                      (JUnit4 ์‚ฌ์šฉ์‹œ ํ•ด๋‹น Annotation์€ Junit์˜ SpringJUnit4ClassRuner์„ ์ƒ์† ๋ฐ›๋Š”                 
                                        @RunWith(SpringRynnver.class)๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋‚˜, JUnit5๋Š” ํ•„์š” ์—†๋‹ค.)
  • @Transactional : Test Case์— ์ด Annotation์ด ์žˆ๋‹ค๋ฉด Test ์‹œ์ž‘ ์ „์— Transation์„ ์‹œ์ž‘ํ•˜๊ณ , Test ์™„๋ฃŒ ๋’ค ํ•ญ์ƒ Roll Back์„
                                  ์‹ค์‹œํ•œ๋‹ค. ๋”ฐ๋ผ์„œ DB์— Data๊ฐ€ ๋‚จ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ Test์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๊ณ , ๋ฐ˜๋ณต Test๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • @commit : Transactionl๊ณผ ๋ฐ˜๋Œ€๋กœ Test๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ์‚ฌ์šฉ๋œ Data๋ฅผ DB์— ๊ทธ๋Œ€๋กœ ์ €์žฅํ•œ๋‹ค.

 


 

์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค

728x90
๋ฐ˜์‘ํ˜•