JunsuLime / spring-cloud-native-explorer

Spring cloud explorer
1 stars 0 forks source link

스프링 부트의 테스트 슬라이스 #22

Closed JunsuLime closed 2 years ago

JunsuLime commented 2 years ago

https://docs.spring.io/spring-boot/docs/current/reference/html/test-auto-configuration.html#test-auto-configuration 스프링 부트는 애플리케이션 테스트 슬라이스를 위한 여러가지 annotation 을 지원한다.

그 중 책에 나온 몇가지와, 진행 중인 프로젝트에서 사용할 만 한 것들을 몇개 사용해보자.

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing

JunsuLime commented 2 years ago

JsonTest

기본적으로 spring context 를 가져오되, 어플리케이션의 모든 컨텍스트를 불러오지는 않는다. Application context 자체를 가져오며, 그 외의 요소는 scan 하지 않는다.

만약 필요한 요소들이 있다면 별도 configuration 을 include 해야함

JacksonTester, GsonTester 등을 test 모듈에서 제공해주고 있다.

JunsuLime commented 2 years ago

WebMvcTest

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing.spring-boot-applications.spring-mvc-tests

Often, @WebMvcTest is limited to a single controller and is used in combination with @MockBean to provide mock implementations for required collaborators.

JunsuLime commented 2 years ago

DataJpaTest

Data JPA tests may also inject a TestEntityManager bean, which provides an alternative to the standard JPA EntityManager that is specifically designed for tests.

DataJpaTest 시 TestEntityManager 를 활용할 수 있다. DataJpaTest 는 JdbcTemplate 을 테스트할 때도 사용 할 수 있다.

JunsuLime commented 2 years ago

RestClientTest

책에서는 RestTemplate 기준으로 작성하는 테스트에 대해서 알려주고있다. WebClient 를 통한 테스트 작성을 하고 싶다면

여기를 보자.

코드는 RestTemplate 기반, WebClient 기반 둘다 짜보도록 하자!

RestTemplate

You can use the @RestClientTest annotation to test REST clients. By default, it auto-configures Jackson, GSON, and Jsonb support, configures a RestTemplateBuilder, and adds support for MockRestServiceServer.

RestTemplateBuilder 로 설정을하는 컴포넌트가 테스트 가능하며, MockRestServiceServer 를 지원해준다

WebClient

https://github.com/spring-projects/spring-framework/blob/main/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java

를 참고해서 테스트 코드 작성

https://github.com/square/okhttp#mockwebserver okhttp 에서 제공하는 mockwebserver 를 사용해서 테스트를 수행한다.

okhttp 의존성과, mockwebserver 의 버전이 동일해야 테스트가 가능하다.

JunsuLime commented 2 years ago

슬라이스 테스트를 위한 SpringBootApplicaiton 의 설정 (TODO: 이어서 적자)

JunsuLime commented 2 years ago

SpringBootTest 는 SpringBootTestContextBootstrapper 를 기반으로 동작한다.

이는 SpringBootTestContextBootstrapper 에 달린 설명이다.

TestContextBootstrapper for Spring Boot. Provides support for @SpringBootTest and may also be used directly or subclassed. Provides the following features over and above DefaultTestContextBootstrapper:
- Uses SpringBootContextLoader as the default context loader.
- Automatically searches for a @SpringBootConfiguration when required.
- Allows custom Environment getProperties(Class) to be defined.
- Provides support for different webEnvironment modes.

SpringBootTest annotation 은 내부적으로 @SpringBootConfiguration 을 가지고 있다. @SpringBootTest 동작 시 @SpringApplication 이 붙은 클래스의 설정 및 boot 어플리케이션이 챙길 autoconfiguration 들을 설정으로 가져온다.

@SpringBootTest
class CloudApplicationTest {

위의 테스트 같은 경우 context 가 정상적으로 올라오는 확인하기위해 짜여진 테스트이며 위의 테스트에서는 cloud 관련 요소를 띄울 수 없기 때문에 test properties 는 spring.cloud 관련 요소를 disable 하였다.