Portfolio/Spring Boot

스프링 부트 8일차 - 스프링 부트 테스트

Foo 2019. 4. 8. 01:04
728x90

1. spring-boot-starter-test를 의존성에 추가

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

테스트 시에만 실행되어야 하기 때문에 스코프는 test로 지정


2. 테스트 코드를 만들때는 Alt + Insert에서 Test를 선택해서 만들면 편함

  * 참고 : https://www.jetbrains.com/help/idea/create-tests.html


3. webEnvironment를 Mock으로 하면 다음과 같은 코드로 테스트할 수 있다.

package me.jun.lee.sample;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
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.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
public class SampleControllerTest {

@Autowired
MockMvc mockMvc;

@Test
public void hello() throws Exception {
mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("hello junhyung"))
.andDo(print());
}

}

소스에 대한 Mock 서버가 생기고 거기에 Mock 클라이언트를 이용해 요청할 수 있다.

  * Mock을 사용할 경우 내장 톰캣을 띄우지 않음.


4. 컨트롤러에서 서비스를 호출하는 식으로 되어있는 코드에서 컨트롤러를 테스트하려면 서비스까지 같이 테스트가 된다.

   근데 서비스까지 호출하고 싶지 않을 수 있다. 단지 컨트롤러만 테스트하고 싶은 경우.

   그럴 때 @MockBean 으로 테스트할 수 있다.

   이것은 서비스를 Mock으로 만들 수 있다.

   다음 코드를 보자

SampleController.java

package me.jun.lee.sample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {

@Autowired
private SampleService sampleService;

@GetMapping("/hello")
public String hello() {
return "hello " + sampleService.getName();
}
}


SampleService.java

package me.jun.lee.sample;

import org.springframework.stereotype.Component;

@Component
public class SampleService {
public String getName() {
return "junhyung";
}
}


SampleControllerTest.java

package me.jun.lee.sample;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SampleControllerTest {

@Autowired
TestRestTemplate testRestTemplate;

@MockBean
SampleService mockSampleService
;

@Test
public void hello() {
when(mockSampleService.getName()).thenReturn("junhyung");

String result = testRestTemplate.getForObject("/hello", String.class);
assertThat(result).isEqualTo("hello junhyung");
}
}

밑줄 친 부분을 보면 SampleService 부분에 @MockBean 어노테이션을 줘서 목업 Bean을 주입받는다.

그리고 테스트 코드에서 when(mockSampleService.getName()).thenReturn("junhyung");

  getName()을 호출할 때 "junhyung"을 반환하도록 하였다.

이렇게하면 컨트롤러에서는 서비스를 호출하지 않고, 목업 서비스에서 junhyung라는 값을 반환받아서 사용한다.

이렇게 해주지 않는다면 컨트롤러에서는 반드시 서비스를 실행해서 값을 가져와야 할 것이다.

  => 실제 비즈니스 로직을 실행한다고 하면 서비스가 매우 무거워질 수 있다. 단지 하나의 서비스를 실행하는 것은 금방 걸리겠지만 서비스가 여러개일 경우 테스트 시간이 너무 오래걸릴 수 있다. 그럴 때 이런 목업 서비스를 사용하면 빠르게 테스트를 진행할 수 있다.


5. 웹 클라이언트를 테스트 해볼 때 사용하기 좋은 것이 WebTestClient이다. (이유:비동기로 요청 가능)

사용하려면 우선 의존성을 추가해줘야한다.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

위와 같이 webflux 의존성 추가

테스트 코드는 다음과 같이 작성

package me.jun.lee.sample;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.mockito.Mockito.when;


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SampleControllerTest {

@Autowired
WebTestClient webTestClient;

@MockBean
SampleService mockSampleService;

@Test
public void hello() {
when(mockSampleService.getName()).thenReturn("junhyung");

webTestClient
.get()
.uri("/hello")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class).isEqualTo("hello junhyung");
}
}


6. @SpringBootTest 어노테이션은 @SpringBootApplication 어노테이션을 찾아가서 연관된 모든 Bean을 등록하여 테스트한다.

   즉 테스트에 필요없는 Bean까지 등록하여 테스트 하기에 좀 무거워질 수 있다.

   이걸 쪼개서 테스트 해보기 위한 방법들이 있음.

  @JsonTest, @WebMvcTest, @WebFluxTest, @DataJpaTest 등등..

  * 자세한 내용은 다음 링크 참고 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html


7. @WebMvcTest를 사용하면 컨트롤러만 테스트 할 수 있는데, 이때 여기서 사용하는 서비스들은 @Component이기 때문에 등록되지 않는다. 그래서 테스트 하려는 컨트롤러에서 의존하고 있는 서비스들이 있다면 반드시 @MockBean으로 목업 서비스를 만들어서 테스트해야한다.


8. OutputCapture를 사용하면 콘솔에 찍히는 로그에서 해당 문자열을 포함하고 있는지 확인할 수 있다.

   * OutputCapture은 jUnit에 포함된 것임.