react-springboot : unit test, integration test
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package com.example.book.domain;
|
package com.example.book.domain;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
|
||||||
@@ -16,4 +18,16 @@ class BookRepositoryUnitTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private BookRepository bookRepository;
|
private BookRepository bookRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void save_테스트() throws Exception {
|
||||||
|
// given
|
||||||
|
Book book = new Book(null, "제목", "저자");
|
||||||
|
|
||||||
|
// when
|
||||||
|
Book bookEntity = bookRepository.save(book);
|
||||||
|
|
||||||
|
// then
|
||||||
|
Assertions.assertEquals(1L, bookEntity.getId());
|
||||||
|
Assertions.assertEquals("제목", bookEntity.getTitle());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,13 @@
|
|||||||
package com.example.book.service;
|
package com.example.book.service;
|
||||||
|
|
||||||
|
import com.example.book.domain.Book;
|
||||||
import com.example.book.domain.BookRepository;
|
import com.example.book.domain.BookRepository;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,4 +24,20 @@ class BookServiceUnitTest {
|
|||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private BookRepository bookRepository;
|
private BookRepository bookRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void 저장하기_테스트() throws Exception {
|
||||||
|
// BODMockito 방식
|
||||||
|
// given
|
||||||
|
Book book = new Book();
|
||||||
|
book.setTitle("제목");
|
||||||
|
book.setAuthor("저자");
|
||||||
|
Mockito.when(bookRepository.save(book)).thenReturn(book);
|
||||||
|
|
||||||
|
// when
|
||||||
|
Book bookEntity = bookService.저장하기(book);
|
||||||
|
|
||||||
|
// then
|
||||||
|
Assertions.assertEquals(bookEntity, book);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ import com.example.book.domain.BookRepository;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -21,8 +22,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
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.MockMvcResultHandlers.print;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
@@ -99,4 +99,81 @@ public class BookControllerIntegrationTests {
|
|||||||
.andDo(print());
|
.andDo(print());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findById_테스트() throws Exception {
|
||||||
|
// given
|
||||||
|
Long id = 2L;
|
||||||
|
List<Book> books = new ArrayList<>();
|
||||||
|
books.add(new Book(null, "스프링", "kim"));
|
||||||
|
books.add(new Book(null, "리액트", "kim"));
|
||||||
|
books.add(new Book(null, "JUnit", "kim"));
|
||||||
|
|
||||||
|
bookRepository.saveAll(books);
|
||||||
|
|
||||||
|
// when
|
||||||
|
ResultActions resultActions = mockMvc.perform(get("/book/{id}", id)
|
||||||
|
.accept(MediaType.APPLICATION_JSON_UTF8));
|
||||||
|
|
||||||
|
// then
|
||||||
|
resultActions
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.title").value("리액트"))
|
||||||
|
.andDo(print());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void update_테스트() throws Exception {
|
||||||
|
// given
|
||||||
|
List<Book> books = new ArrayList<>();
|
||||||
|
books.add(new Book(null, "스프링", "kim"));
|
||||||
|
books.add(new Book(null, "리액트", "kim"));
|
||||||
|
books.add(new Book(null, "JUnit", "kim"));
|
||||||
|
|
||||||
|
bookRepository.saveAll(books);
|
||||||
|
|
||||||
|
Long id = 2L;
|
||||||
|
Book book = new Book(null, "SQL", "kim");
|
||||||
|
String content = new ObjectMapper().writeValueAsString(book);
|
||||||
|
|
||||||
|
// when
|
||||||
|
ResultActions resultActions = mockMvc.perform(put("/book/{id}", id)
|
||||||
|
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||||
|
.content(content)
|
||||||
|
.accept(MediaType.APPLICATION_JSON_UTF8));
|
||||||
|
|
||||||
|
// then
|
||||||
|
resultActions
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.id").value(2L))
|
||||||
|
.andExpect(jsonPath("$.title").value("SQL"))
|
||||||
|
.andDo(print());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void delete_테스트() throws Exception {
|
||||||
|
// given
|
||||||
|
List<Book> books = new ArrayList<>();
|
||||||
|
books.add(new Book(null, "스프링", "kim"));
|
||||||
|
books.add(new Book(null, "리액트", "kim"));
|
||||||
|
books.add(new Book(null, "JUnit", "kim"));
|
||||||
|
|
||||||
|
bookRepository.saveAll(books);
|
||||||
|
|
||||||
|
Long id = 1L;
|
||||||
|
|
||||||
|
// when
|
||||||
|
ResultActions resultActions = mockMvc.perform(delete("/book/{id}", id)
|
||||||
|
.accept(MediaType.TEXT_PLAIN));
|
||||||
|
|
||||||
|
// then
|
||||||
|
resultActions
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andDo(print());
|
||||||
|
|
||||||
|
String result = resultActions.andReturn()
|
||||||
|
.getResponse().getContentAsString();
|
||||||
|
|
||||||
|
Assertions.assertEquals("ok", result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,14 @@ import com.example.book.service.BookService;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.MvcResult;
|
||||||
import org.springframework.test.web.servlet.ResultActions;
|
import org.springframework.test.web.servlet.ResultActions;
|
||||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||||
|
|
||||||
@@ -18,8 +20,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
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.MockMvcResultHandlers.*;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
@@ -94,4 +95,46 @@ class BookControllerUnitTest {
|
|||||||
.andExpect(jsonPath("$.title").value("자바"))
|
.andExpect(jsonPath("$.title").value("자바"))
|
||||||
.andDo(print());
|
.andDo(print());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void update_테스트() throws Exception {
|
||||||
|
// given
|
||||||
|
Long id = 1L;
|
||||||
|
Book book = new Book(null, "SQL", "kim");
|
||||||
|
String content = new ObjectMapper().writeValueAsString(book);
|
||||||
|
when(bookService.수정하기(id, book)).thenReturn(new Book(1L, "SQL", "kim"));
|
||||||
|
|
||||||
|
// when
|
||||||
|
ResultActions resultActions = mockMvc.perform(put("/book/{id}", id)
|
||||||
|
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||||
|
.content(content)
|
||||||
|
.accept(MediaType.APPLICATION_JSON_UTF8));
|
||||||
|
|
||||||
|
// then
|
||||||
|
resultActions
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.title").value("SQL"))
|
||||||
|
.andDo(print());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void delete_테스트() throws Exception {
|
||||||
|
// given
|
||||||
|
Long id = 1L;
|
||||||
|
when(bookService.삭제하기(id)).thenReturn("ok");
|
||||||
|
|
||||||
|
// when
|
||||||
|
ResultActions resultActions = mockMvc.perform(delete("/book/{id}", id)
|
||||||
|
.accept(MediaType.TEXT_PLAIN));
|
||||||
|
|
||||||
|
// then
|
||||||
|
resultActions
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andDo(print());
|
||||||
|
|
||||||
|
String result = resultActions.andReturn()
|
||||||
|
.getResponse().getContentAsString();
|
||||||
|
|
||||||
|
Assertions.assertEquals("ok", result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user