From a350f8f707867044cb969fd35173237f23ded1fd Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 5 Feb 2021 21:07:48 +0900 Subject: [PATCH] react-springboot : unit test, integration test --- .../book/domain/BookRepositoryUnitTest.java | 14 ++++ .../book/service/BookServiceUnitTest.java | 20 +++++ .../web/BookControllerIntegrationTests.java | 81 ++++++++++++++++++- .../book/web/BookControllerUnitTest.java | 47 ++++++++++- 4 files changed, 158 insertions(+), 4 deletions(-) diff --git a/react-springboot/server/src/test/java/com/example/book/domain/BookRepositoryUnitTest.java b/react-springboot/server/src/test/java/com/example/book/domain/BookRepositoryUnitTest.java index 3ec180b0..762f5563 100644 --- a/react-springboot/server/src/test/java/com/example/book/domain/BookRepositoryUnitTest.java +++ b/react-springboot/server/src/test/java/com/example/book/domain/BookRepositoryUnitTest.java @@ -1,5 +1,7 @@ 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.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; @@ -16,4 +18,16 @@ class BookRepositoryUnitTest { @Autowired 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()); + } } \ No newline at end of file diff --git a/react-springboot/server/src/test/java/com/example/book/service/BookServiceUnitTest.java b/react-springboot/server/src/test/java/com/example/book/service/BookServiceUnitTest.java index 0d247d92..53921dad 100644 --- a/react-springboot/server/src/test/java/com/example/book/service/BookServiceUnitTest.java +++ b/react-springboot/server/src/test/java/com/example/book/service/BookServiceUnitTest.java @@ -1,9 +1,13 @@ package com.example.book.service; +import com.example.book.domain.Book; 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.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; /** @@ -20,4 +24,20 @@ class BookServiceUnitTest { @Mock 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); + } } \ No newline at end of file diff --git a/react-springboot/server/src/test/java/com/example/book/web/BookControllerIntegrationTests.java b/react-springboot/server/src/test/java/com/example/book/web/BookControllerIntegrationTests.java index e3178512..8cc22f2a 100644 --- a/react-springboot/server/src/test/java/com/example/book/web/BookControllerIntegrationTests.java +++ b/react-springboot/server/src/test/java/com/example/book/web/BookControllerIntegrationTests.java @@ -5,6 +5,7 @@ import com.example.book.domain.BookRepository; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -21,8 +22,7 @@ import java.util.ArrayList; import java.util.List; 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.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 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; @@ -99,4 +99,81 @@ public class BookControllerIntegrationTests { .andDo(print()); } + + @Test + public void findById_테스트() throws Exception { + // given + Long id = 2L; + List 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 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 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); + } } diff --git a/react-springboot/server/src/test/java/com/example/book/web/BookControllerUnitTest.java b/react-springboot/server/src/test/java/com/example/book/web/BookControllerUnitTest.java index f40545de..2c2c41cf 100644 --- a/react-springboot/server/src/test/java/com/example/book/web/BookControllerUnitTest.java +++ b/react-springboot/server/src/test/java/com/example/book/web/BookControllerUnitTest.java @@ -5,12 +5,14 @@ import com.example.book.service.BookService; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; 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.MvcResult; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; @@ -18,8 +20,7 @@ import java.util.ArrayList; import java.util.List; 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.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 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; @@ -94,4 +95,46 @@ class BookControllerUnitTest { .andExpect(jsonPath("$.title").value("자바")) .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); + } } \ No newline at end of file