Skip to content

Commit

Permalink
Merge pull request #24 from DAY0522/feature/giftshop-list-test-code
Browse files Browse the repository at this point in the history
독립적인 test code DB 환경 구축(h2 database)
  • Loading branch information
chae52 authored Aug 31, 2023
2 parents 7b2c189 + 2a8081f commit 7cf5238
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 10 deletions.
8 changes: 4 additions & 4 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
### PR 타입(하나 이상의 PR 타입을 선택해주세요)
-[] 기능 추가
-[] 기능 삭제
-[] 버그 수정
-[] 의존성, 환경 변수, 빌드 관련 코드 업데이트
- [ ] 기능 추가
- [ ] 기능 삭제
- [ ] 버그 수정
- [ ] 의존성, 환경 변수, 빌드 관련 코드 업데이트

### 반영 브랜치
ex) feat/login -> dev
Expand Down
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ dependencies {
implementation 'io.sentry:sentry-logback:6.25.2'
//swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'


//h2
implementation 'com.h2database:h2:1.4.200'
}

tasks.named('test') {
useJUnitPlatform()
}

jar { enabled = false }

12 changes: 9 additions & 3 deletions src/main/java/com/management/web/domain/GiftcardProduct.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.management.web.domain;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
Expand All @@ -25,7 +26,12 @@ public class GiftcardProduct {

@Column(name = "sale_YN")
private Boolean saleYN;
//
// @OneToMany(mappedBy = "giftcardProduct")
// private List<GiftcardOrder> giftcardOrderList;

@Builder
public GiftcardProduct(String name, Long price, String image, Boolean saleYN) {
this.name = name;
this.price = price;
this.image = image;
this.saleYN = saleYN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface GiftcardProductRepository extends JpaRepository<GiftcardProduct

@Query("SELECT p from GiftcardProduct p where p.saleYN = true order by p.price ASC ")
List<GiftcardProduct> findBySaleYNOrderByPriceAsc(Boolean saleYN);

GiftcardProduct save(GiftcardProduct giftcardProduct);
}
68 changes: 68 additions & 0 deletions src/test/java/com/management/web/api/GiftshopTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.management.web.api;

import com.management.chatbot.service.MemberService;
import com.management.chatbot.service.dto.MemberResponseDto;
import com.management.chatbot.service.dto.MemberSaveRequestDto;
import com.management.web.domain.GiftcardProduct;
import com.management.web.repository.GiftcardProductRepository;
import com.management.web.service.GiftcardService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
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.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@SpringBootTest
@AutoConfigureMockMvc
public class GiftshopTest {

@Autowired
private MemberService memberService;

@Autowired
private GiftcardProductRepository giftcardProductRepository;

@Autowired
MockMvc mvc;

String kakaoId = "test";

@BeforeEach
void setUp() {
memberService.save(MemberSaveRequestDto.builder()
.kakaoId(kakaoId)
.username("test")
.reward(0L)
.savedMoney(0L)
.build());

giftcardProductRepository.save(GiftcardProduct.builder()
.name("test")
.price(10000L)
.saleYN(true)
.build());
}
@Test
@DisplayName("기프티콘 목록 API 테스트")
void getGiftcardList() throws Exception {
// given
MemberResponseDto member = memberService.findByKakaoId(kakaoId);
String username = member.getUsername();
Long reward = member.getReward();

// when

// then
mvc.perform(
MockMvcRequestBuilders.get("/shop/{kakaoId}", kakaoId))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.member.username").value(username))
.andExpect(MockMvcResultMatchers.jsonPath("$.member.reward").value(reward))
.andDo(MockMvcResultHandlers.print());
}
}
18 changes: 18 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:public;MODE=PostgreSQL;INIT=CREATE DOMAIN IF NOT EXISTS JSONB AS JSON;
username: sa
password:
h2:
console:
enabled: true
profiles:
active: local
jpa:
database-platform: org.hibernate.dialect.H2Dialect
database: h2
hibernate:
ddl-auto: create-drop
open-in-view: false
show-sql: true

0 comments on commit 7cf5238

Please sign in to comment.