react-springboot : controller test (unit, integration)
This commit is contained in:
@@ -15,3 +15,4 @@ spring:
|
||||
open-in-view: true
|
||||
hibernate:
|
||||
ddl-auto: create
|
||||
show-sql: true
|
||||
@@ -10,7 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Transactional
|
||||
@AutoConfigureTestDatabase(replace = Replace.ANY) // Replace.ANY 가짜 DB로 테스트, Replace.NONE 실제 DB로 테스트
|
||||
@DataJpaTest // Repository 들을 spring container 에 등록해줌
|
||||
@DataJpaTest() // Repository 들을 spring container 에 등록해줌
|
||||
class BookRepositoryUnitTest {
|
||||
|
||||
@Autowired
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.example.book.web;
|
||||
|
||||
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.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 통합테스트 ( 모든 Bean 들을 IoC에 올리고 테스트 하는 것 )
|
||||
* WebEnvironment.MOCK = 실제 톰캣이 아닌 다른 톰캣으로 테스트
|
||||
* WebEnvironment.RANDOM_PORT = 실제 톰캣으로 테스트
|
||||
* @AutoConfigureMockMvc = MockMvc 를 IoC에 등록
|
||||
* @Transactional = 각 테스트 함수가 종료되면 트랜잭션을 rollback
|
||||
*
|
||||
*/
|
||||
|
||||
@Transactional
|
||||
@AutoConfigureMockMvc
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
|
||||
public class BookApplicationIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.example.book.web;
|
||||
|
||||
import com.example.book.domain.Book;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/**
|
||||
* 통합테스트 ( 모든 Bean 들을 IoC에 올리고 테스트 하는 것 )
|
||||
* WebEnvironment.MOCK = 실제 톰캣이 아닌 다른 톰캣으로 테스트
|
||||
* WebEnvironment.RANDOM_PORT = 실제 톰캣으로 테스트
|
||||
* @AutoConfigureMockMvc = MockMvc 를 IoC에 등록
|
||||
* @Transactional = 각 테스트 함수가 종료되면 트랜잭션을 rollback
|
||||
*
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Transactional
|
||||
@AutoConfigureMockMvc
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
|
||||
public class BookControllerIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
// BDDMockito 패턴 given, when, then
|
||||
@Test
|
||||
public void save_테스트() throws Exception {
|
||||
log.info("save 테스트 시작 ==================================");
|
||||
// given ( 테스트를 하기 위한 준비 )
|
||||
Book book = new Book(null, "스프링", "kim");
|
||||
String content = new ObjectMapper().writeValueAsString(book);
|
||||
|
||||
// when ( 테스트 실행 )
|
||||
ResultActions resultActions = mockMvc.perform(post("/book")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(content)
|
||||
.accept(MediaType.APPLICATION_JSON_UTF8));
|
||||
|
||||
// then ( 검증 )
|
||||
resultActions
|
||||
.andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("$.title").value("스프링"))
|
||||
.andDo(print());
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,55 @@
|
||||
package com.example.book.web;
|
||||
|
||||
import com.example.book.domain.Book;
|
||||
import com.example.book.service.BookService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
// 단위테스트 ( Controller 관련 로직만 테스트 - Filter, ControllerAdvice , ... )
|
||||
|
||||
@Slf4j
|
||||
@WebMvcTest
|
||||
class BookControllerUnitTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean // IoC 환경에 등록
|
||||
private BookService bookService;
|
||||
|
||||
// BDDMockito 패턴 given, when, then
|
||||
@Test
|
||||
public void save_테스트() throws Exception {
|
||||
log.info("save 테스트 시작 ==================================");
|
||||
// given ( 테스트를 하기 위한 준비 )
|
||||
Book book = new Book(null, "스프링", "kim");
|
||||
String content = new ObjectMapper().writeValueAsString(book);
|
||||
when(bookService.저장하기(book)).thenReturn(new Book(1L, "스프링", "kim"));
|
||||
|
||||
// when ( 테스트 실행 )
|
||||
ResultActions resultActions = mockMvc.perform(post("/book")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(content)
|
||||
.accept(MediaType.APPLICATION_JSON));
|
||||
|
||||
// then ( 검증 )
|
||||
resultActions
|
||||
.andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("$.title").value("스프링"))
|
||||
.andDo(print());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user