책/전문가를 위한 스프링 5

Spring 테스트 코드 적용하기(JUnit, TDD)

개발하는 민우 2022. 4. 28. 16:13

TDD란?


테스트 주도 개발

테스트를 먼저 설계 및 구축 후 테스트를 통과할 수 있는 코드를 짜는 것

 

애자일 개발 방식 중 하나

- 코드 설계시 원하는 단계 목표에 대해 설정하여, 프로그램 결정 방향의 갭을 줄임

- 최초 목표에 맞춘 테스트를 구축하여 그에 맞게 코드를 설계하기 때문에 적은 의견 충돌

 

목적

- 코드의 안정성

- 기능 추가, 변경 과정에서 Side-Effect를 줄일 수 있다.

- 코드 목적을 명확하게 표현 가능


JUnit이란?

Java 진영의 대표적 Test Framework

 

 

단위 테스트를 위한 도구 제공

- 단위 테스트란?

  -코드의 특정 모듈이 의도된 대로 동작하는지 테스트하는 절차

  -모든 함수와 메소드에 대한 각각의 테스트 케이스를 작성

 

F.I.R.S.T 원칙

- Fast: 테스트코드의 실행은 빠르게 진행되어야 함

- Independent: 독립적인 테스트가 가능해야 함

- Repeatable: 테스트는 매번 같은 결과를 만들어야 함

- Self-Validating: 테스트는 그 자체로 실행해서 결과를 확인할 수 있어야 함

- Timely: 단위 테스트는 코드가 완성되기 전에 구성하고, 테스트가 가능해야 함(TDD/테스트 주도 개발의 원칙)

 

 

어노테이션 기반, 단정문(Assert)으로 테스트 케이스의 기대값에 대해 수행 결과 확인 가능

 


JUnit LifeCycle Annotation

 

Annotation

@Test: 테스트용 메소드를 표현하는 어노테이션

@BeforeEach: 각 테스트 메소드가 시작되기 전에 실행되어야 하는 메소드를 표현 

@AfterEach: 각 테스트 메소드가 시작된 후 실행되어야 하는 메소드를 표현

@BeforeAll: 테스트 시작 전에 실행되어야 하는 메소드를 표현(static 처리가 필요한 메서드에 사용)

@AfterAll: 테스트 종료 후에 실행되어야 하는 메소드를 표현(static 처리가 필요한 메서드에 사용)

 


JUnit Main Annotation

 

@SpringBootTest

- 통합 테스트 용도로 사용됨

- @SpringBootApplication을 찾아가 하위의 모든 Bean을 스캔하여 로드함

- 그 후 Test Application Context를 받아서 로드된 Bean을 추가, 만약 MockBean으로 추가된 Bean이 있다면 해당 Bean을 찾아서 교체

 

@ExtendWIth

- @ExtendWith는 메인으로 실행될 Class를 지정할 수 있음

- @SpringBootTest는 기본적으로 @ExtendWith를 추가하고 있음

 

@WebMvcTest(Class명.class)

- 작성된 클래스만 로드하여 테스트 진행

- 컨트롤러 관련 코드만 테스트할 경우 사용

 

@Autowired about Mockbean

- Controller의 API를 테스트하는 용도인 MockMvc 객체를 선언하고, Autowired를 사용해 Mockmvc 객체 주입

- perform() 메소드를 활용하여 컨트롤러의 동작을 확인할 수 있음

.andExpect() 기대값 , andDo() 어떤 행위를 할 지 , andReturn() 어떤 값을 돌려받는지 등의 메소드를 같이 활용함(Restful 통신)

 

@MockBean

- 테스트할 클래스에서 주입 받고 있는 객체에 대해 가짜 객체를 생성해주는 어노테이션

- 해당 객체는 실제 행위 X

- given() 메소드를 활용하여 가짜 객체 동작을 정의하여 사용 가능

 

@Import

- 필요한 Class들을 Configuration으로 만들어 사용할 수 있음

- Import된 클래스는 주입(@Autowired)로 사용가능

 


 

통합 테스트

통합 테스트는 여러 기능을 조합하여 전체 비지니스 로직이 제대로 동작하는지 확인하는 것

 

통합 테스트의 경우 @SpringBootTest를 사용하여 진행

- @SpringBootTest는 @SpringBootApplication을 찾아가서 모든 Bean 로드

- 대규모 프로젝트에서 사용할 경우, 테스트 실행시 모든 빈을 스캔, 로드 작업이 반복되어 매번 무거운 작업 시행됨.


스프링 프로젝트 생성 시 자동 생성되는 TestClass

@SpringBootTest
class AroundHubSpringBootApplicationTests {

    @Test
    void contextLoads() {
    }

}

통합테스트 어노테이션이 붙어 있어, 모든 컨텍스트가 로드되어 있는지 기본 생성 테스트합니다.

 


테스트 시 main 경로와 test 경로가 같은 위치에 있어야 함

 

public class TestLifeCycle {

    @BeforeAll // 테스트 시작 전 메서드
    static void beforeAll() {
        System.out.println("## BeforeAll Annotation 호출 ##");
        System.out.println();
    }

    @AfterAll // 테스트 종료 후 메서드
    static void afterAll() {
        System.out.println("## afterAll Annotation 호출 ##");
        System.out.println();
    }

    @BeforeEach // 각 테스트 메서드 시작 전 메서드
    void beforeEach() {
        System.out.println("## beforeEach Annotation 호출 ##");
        System.out.println();
    }

    @AfterEach  // 각 테스트 메서드 시작 후 메서드
    void afterEach() {
        System.out.println("## afterEach Annotation 호출 ##");
        System.out.println();
    }

    @Test
    void test1() {
        System.out.println("## test1 시작 ##");
        System.out.println();
    }

    @Test
    @DisplayName("Test Case 2!!!") // 테스트 시 실행된 테스트 이름 설정 가능
    void test2() {
        System.out.println("## test2 시작 ##");
        System.out.println();
    }

    @Test
    @Disabled
        // Disabled Annotation : 테스트를 실행하지 않게 설정하는 어노테이션
    void test3() {
        System.out.println("## test3 시작 ##");
        System.out.println();
    }

}

[실행 결과]

## BeforeAll Annotation 호출 ##

## beforeEach Annotation 호출 ##

## test1 시작 ##

## afterEach Annotation 호출 ##

## beforeEach Annotation 호출 ##

## test2 시작 ##

## afterEach Annotation 호출 ##


void studio.thinkground.aroundhub.test.TestLifeCycle.test3() is @Disabled
## afterAll Annotation 호출 ##

ProductControllerTest.class

@WebMvcTest(ProductController.class) // WebMvcTest에 매개변수에 테스트하고자 하는 클래스 입력
//@AutoConfigureWebMvc // 이 어노테이션을 통해 MockMvc를 Builder 없이 주입받을 수 있음
public class ProductControllerTest {

  @Autowired
  private MockMvc mockMvc; //  Controller의 API를 테스트하는 용도인 MockMvc 객체를 선언하고, Autowired를 사용해 Mockmvc 객체 주입

  // ProductController에서 의존하는 productService 객체에 대해 가짜 Mock 객체를 생성해줌
  @MockBean
  ProductServiceImpl productService;

 
  @Test // 메서드는 각각 테스트의 주체이므로 test 어노테이션 추가
  @DisplayName("Product 데이터 가져오기 테스트") // 테스트 이름 설정
  void getProductTest() throws Exception {

    // Mockito(Mock 객체 생성 도와주고, 사용하는 데 도와줌)
    // given : Mock 객체로 만든 productService 생성, 특정 상황에서 해야하는 행위를 정의하는 메소드
    // 12315 product를 받았다면 -> DTO 생성해서 반환
    given(productService.getProduct("12315")).willReturn(
        new ProductDto("15871", "pen", 5000, 2000));

    String productId = "12315";

    // andExpect : 기대하는 값이 나왔는지 체크해볼 수 있는 메소드
    // perform: RestApi 테스트 할 수 있는 환경 만들어줌
    mockMvc.perform(
            get("/api/v1/product-api/product/" + productId))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.productId").exists()) // json path의 depth가 깊어지면 .을 추가하여 탐색할 수 있음 (ex : $.productId.productIdName)
        .andExpect(jsonPath("$.productName").exists())
        .andExpect(jsonPath("$.productPrice").exists())
        .andExpect(jsonPath("$.productStock").exists())
        .andDo(print());

    // verify : 해당 객체의 메소드가 실행되었는지 체크해줌
    verify(productService).getProduct("12315");
  }


  // http://localhost:8080/api/v1/product-api/product
  @Test
  @DisplayName("Product 데이터 생성 테스트")
  void createProductTest() throws Exception {
    //Mock 객체에서 특정 메소드가 실행되는 경우 실제 Return을 줄 수 없기 때문에 아래와 같이 가정 사항을 만들어줌
    given(productService.saveProduct("15871", "pen", 5000, 2000)).willReturn(
        new ProductDto("15871", "pen", 5000, 2000));

    ProductDto productDto = ProductDto.builder().productId("15871").productName("pen")
        .productPrice(5000).productStock(2000).build();
    Gson gson = new Gson();
    // 구글에서 만든 Gson (Json to String)
    String content = gson.toJson(productDto);

    // 아래 코드로 json 형태 변경 작업을 대체할 수 있음
    // String json = new ObjectMapper().writeValueAsString(productDto);

    mockMvc.perform(
            post("/api/v1/product-api/product")
                .content(content)
                // 컨텐트 타입 설정
                .contentType(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        // $ 로 json의 키값을 확인 가능
        .andExpect(jsonPath("$.productId").exists())
        .andExpect(jsonPath("$.productName").exists())
        .andExpect(jsonPath("$.productPrice").exists())
        .andExpect(jsonPath("$.productStock").exists())
        .andDo(print());

	// 해당 객체 메서드가 실행되었는지 확인 가능
    verify(productService).saveProduct("15871", "pen", 5000, 2000);
  }

}

 

PostController.class

public class ProductController {

    private final Logger LOGGER = LoggerFactory.getLogger(ProductController.class);
    private final ProductService productService;

    @Autowired
    public ProductController(ProductService productService) {
        this.productService = productService;
    }
....

ProductServiceImplTest

 

// 내가 어떠한 객체를 받아올지 정의(Bean 값 로드)
//@SpringBootTest(classes = {ProductDataHandlerImpl.class, ProductServiceImpl.class}) 
// ExtendWIth가 SpringBootTest에 포함
@ExtendWith(SpringExtension.class)
@Import({ProductDataHandlerImpl.class, ProductServiceImpl.class})
public class ProductServiceImplTest {

 // ProductServiceImpl에서 의존하고 있는 productDataHandler에 대해 가짜 객체(Mock) 생성
  @MockBean
  ProductDataHandlerImpl productDataHandler;

 // 서비스에 관련된 통신(서비스 테스트) 할 것 이기 때문에 productService 생성하고 주입
  @Autowired
  ProductServiceImpl productService;

  @Test
  public void getProductTest() {
    //given
    Mockito.when(productDataHandler.getProductEntity("123"))
        .thenReturn(new Product("123", "pen", 2000, 3000));

    ProductDto productDto = productService.getProduct("123");

    Assertions.assertEquals(productDto.getProductId(), "123");
    Assertions.assertEquals(productDto.getProductName(), "pen");
    Assertions.assertEquals(productDto.getProductPrice(), 2000);
    Assertions.assertEquals(productDto.getProductStock(), 3000);

    verify(productDataHandler).getProductEntity("123");
  }

  @Test
  public void saveProductTest() {
    //given
    Mockito.when(productDataHandler.saveProductEntity("123", "pen", 2000, 3000))
        .thenReturn(new Product("123", "pen", 2000, 3000));

    ProductDto productDto = productService.saveProduct("123", "pen", 2000, 3000);

    Assertions.assertEquals(productDto.getProductId(), "123");
    Assertions.assertEquals(productDto.getProductName(), "pen");
    Assertions.assertEquals(productDto.getProductPrice(), 2000);
    Assertions.assertEquals(productDto.getProductStock(), 3000);

    verify(productDataHandler).saveProductEntity("123", "pen", 2000, 3000);
  }

ProductService

@Service
public class ProductServiceImpl implements ProductService {

    private final Logger LOGGER = LoggerFactory.getLogger(ProductServiceImpl.class);

    ProductDataHandler productDataHandler;

    @Autowired
    public ProductServiceImpl(ProductDataHandler productDataHandler) {
        this.productDataHandler = productDataHandler;
    }
 
 ...