Skip to content

Commit

Permalink
Merge pull request #50 from endless-horses/feature#41-pattern-test
Browse files Browse the repository at this point in the history
pattern test code 구현 close #41
  • Loading branch information
SubiHwang authored Feb 19, 2024
2 parents fa052a6 + 60f6a1c commit 025bb21
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ public class PatternController {
public List<PatternListResponseDto> findAll() {
return patternService.list();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@NoArgsConstructor
Expand All @@ -22,4 +23,5 @@ public PatternListResponseDto(Pattern pattern) {
this.imageUrl = pattern.getImageUrl();
this.explanation = pattern.getExplanation();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,4 +29,5 @@ public class Pattern {

@OneToMany(mappedBy = "pattern")
private List<Result> results = new ArrayList<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<PatternListResponseDto> 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());

}

}

0 comments on commit 025bb21

Please sign in to comment.