From 30c9d268cac6111e72905fea9fe1133d06efd72e Mon Sep 17 00:00:00 2001 From: Subi Hwang Date: Wed, 7 Feb 2024 18:06:38 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20pattern=20test=20code=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20#41?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oot/custom/pattern/entity/Pattern.java | 1 + .../oot/pattern/PatternServiceTest.java | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java diff --git a/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java b/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java index 1a19186..f4f0a32 100644 --- a/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java +++ b/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java @@ -26,4 +26,5 @@ public class Pattern { @OneToMany(mappedBy = "pattern") private List results = new ArrayList<>(); + } diff --git a/src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java b/src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java new file mode 100644 index 0000000..6ec0962 --- /dev/null +++ b/src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java @@ -0,0 +1,58 @@ +package com.endlesshorses.oot.pattern; + +import com.endlesshorses.oot.custom.pattern.dto.PatternListResponseDto; +import com.endlesshorses.oot.custom.pattern.entity.Pattern; +import com.endlesshorses.oot.custom.pattern.repository.PatternRepository; +import com.endlesshorses.oot.custom.pattern.service.PatternService; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.Arrays; +import java.util.List; + +import static org.mockito.Mockito.when; + +@SpringBootTest +public class PatternServiceTest { + @Mock + private PatternRepository patternRepository; + + @InjectMocks + private PatternService patternService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testList() { + + Pattern pattern1 = new Pattern(); + Pattern pattern2 = new Pattern(); + + + List patternList = Arrays.asList(pattern1, pattern2); + + //패턴 레포지토리의 findAll() 메서드가 호출될 때 패턴 엔티티 목록 반환하도록 설정 + when(patternRepository.findAll()).thenReturn(patternList); + + // 패턴 서비스의 list() 메서드 호출 + List result = patternService.list(); + + // 결과 + if (!result.isEmpty()) { + Assertions.assertEquals("리브패턴", result.get(0).getName()); + Assertions.assertEquals("리브러그패턴", result.get(1).getName()); + } else { + // 패턴 목록이 비어있는 경우에 대한 예외 처리 + Assertions.fail("패턴 목록이 비어있습니다."); + } + } + +} From 60f6a1c678fbbb62f27264c89d11283dbba75814 Mon Sep 17 00:00:00 2001 From: Subi Hwang Date: Mon, 19 Feb 2024 16:16:46 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20pattern=20test=20code=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20#41?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../pattern/controller/PatternController.java | 1 + .../pattern/dto/PatternListResponseDto.java | 2 + .../oot/custom/pattern/entity/Pattern.java | 3 + .../pattern/service/PatternService.java | 2 + .../controller/PatternControllerTest.java | 60 +++++++++++++++++++ .../oot/pattern/PatternServiceTest.java | 58 ------------------ 7 files changed, 69 insertions(+), 58 deletions(-) create mode 100644 src/test/java/com/endlesshorses/oot/custom/pattern/controller/PatternControllerTest.java delete mode 100644 src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java diff --git a/build.gradle b/build.gradle index f4371ba..0e32ead 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.4' + testImplementation 'junit:junit:4.13.1' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'com.mysql:mysql-connector-j' diff --git a/src/main/java/com/endlesshorses/oot/custom/pattern/controller/PatternController.java b/src/main/java/com/endlesshorses/oot/custom/pattern/controller/PatternController.java index ca5b267..c7e27b7 100644 --- a/src/main/java/com/endlesshorses/oot/custom/pattern/controller/PatternController.java +++ b/src/main/java/com/endlesshorses/oot/custom/pattern/controller/PatternController.java @@ -29,4 +29,5 @@ public class PatternController { public List findAll() { return patternService.list(); } + } diff --git a/src/main/java/com/endlesshorses/oot/custom/pattern/dto/PatternListResponseDto.java b/src/main/java/com/endlesshorses/oot/custom/pattern/dto/PatternListResponseDto.java index 55e739c..5f30f2f 100644 --- a/src/main/java/com/endlesshorses/oot/custom/pattern/dto/PatternListResponseDto.java +++ b/src/main/java/com/endlesshorses/oot/custom/pattern/dto/PatternListResponseDto.java @@ -4,6 +4,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; @Getter @NoArgsConstructor @@ -22,4 +23,5 @@ public PatternListResponseDto(Pattern pattern) { this.imageUrl = pattern.getImageUrl(); this.explanation = pattern.getExplanation(); } + } diff --git a/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java b/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java index f4f0a32..4dbef6a 100644 --- a/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java +++ b/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java @@ -2,14 +2,17 @@ import com.endlesshorses.oot.custom.result.entity.Result; import jakarta.persistence.*; +import jdk.jshell.Snippet; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import java.util.ArrayList; import java.util.List; @Entity @Getter +@Setter @NoArgsConstructor public class Pattern { @Id diff --git a/src/main/java/com/endlesshorses/oot/custom/pattern/service/PatternService.java b/src/main/java/com/endlesshorses/oot/custom/pattern/service/PatternService.java index 3ec1bb2..5cb09e8 100644 --- a/src/main/java/com/endlesshorses/oot/custom/pattern/service/PatternService.java +++ b/src/main/java/com/endlesshorses/oot/custom/pattern/service/PatternService.java @@ -2,7 +2,9 @@ import com.endlesshorses.oot.custom.pattern.dto.PatternListResponseDto; import com.endlesshorses.oot.custom.pattern.repository.PatternRepository; +import lombok.NoArgsConstructor; import lombok.RequiredArgsConstructor; +import lombok.Setter; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/src/test/java/com/endlesshorses/oot/custom/pattern/controller/PatternControllerTest.java b/src/test/java/com/endlesshorses/oot/custom/pattern/controller/PatternControllerTest.java new file mode 100644 index 0000000..668a707 --- /dev/null +++ b/src/test/java/com/endlesshorses/oot/custom/pattern/controller/PatternControllerTest.java @@ -0,0 +1,60 @@ +package com.endlesshorses.oot.custom.pattern.controller; + +import com.endlesshorses.oot.custom.pattern.dto.PatternListResponseDto; +import com.endlesshorses.oot.custom.pattern.entity.Pattern; +import com.endlesshorses.oot.custom.pattern.service.PatternService; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +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.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Arrays; +import java.util.List; +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.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +public class PatternControllerTest { + + @Autowired + private MockMvc mockMvc; + @MockBean + private PatternService patternService; + private List patternList; + + + @Test + @DisplayName("패턴 데이터 가져오기 테스트") + void getPatternResponse() throws Exception { + + //given + Pattern pattern1 = new Pattern(); + pattern1.setId(1004L); + pattern1.setName("리브 패턴"); + pattern1.setPrice(1004L); + pattern1.setImageUrl("http://~"); + pattern1.setExplanation("테스트!"); + + + PatternListResponseDto patternDto = new PatternListResponseDto(pattern1); + patternList = Arrays.asList(patternDto); + Mockito.when(patternService.list()).thenReturn(patternList); + + + mockMvc.perform(get("/api/patterns") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$[0].name").value(patternList.get(0).getName())) + .andExpect(status().isOk()) + .andDo(print()); + + } + +} diff --git a/src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java b/src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java deleted file mode 100644 index 6ec0962..0000000 --- a/src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.endlesshorses.oot.pattern; - -import com.endlesshorses.oot.custom.pattern.dto.PatternListResponseDto; -import com.endlesshorses.oot.custom.pattern.entity.Pattern; -import com.endlesshorses.oot.custom.pattern.repository.PatternRepository; -import com.endlesshorses.oot.custom.pattern.service.PatternService; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.springframework.boot.test.context.SpringBootTest; - -import java.util.Arrays; -import java.util.List; - -import static org.mockito.Mockito.when; - -@SpringBootTest -public class PatternServiceTest { - @Mock - private PatternRepository patternRepository; - - @InjectMocks - private PatternService patternService; - - @BeforeEach - public void setUp() { - MockitoAnnotations.openMocks(this); - } - - @Test - public void testList() { - - Pattern pattern1 = new Pattern(); - Pattern pattern2 = new Pattern(); - - - List patternList = Arrays.asList(pattern1, pattern2); - - //패턴 레포지토리의 findAll() 메서드가 호출될 때 패턴 엔티티 목록 반환하도록 설정 - when(patternRepository.findAll()).thenReturn(patternList); - - // 패턴 서비스의 list() 메서드 호출 - List result = patternService.list(); - - // 결과 - if (!result.isEmpty()) { - Assertions.assertEquals("리브패턴", result.get(0).getName()); - Assertions.assertEquals("리브러그패턴", result.get(1).getName()); - } else { - // 패턴 목록이 비어있는 경우에 대한 예외 처리 - Assertions.fail("패턴 목록이 비어있습니다."); - } - } - -}