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 1a19186..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 @@ -26,4 +29,5 @@ public class Pattern { @OneToMany(mappedBy = "pattern") private List results = new ArrayList<>(); + } 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()); + + } + +}