Skip to content

Commit

Permalink
test: 테스트코드 환경 세팅 (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekks authored Aug 3, 2024
1 parent 66331c4 commit 467c1b8
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ project(":layer-domain") {

testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'

}
}

Expand Down
2 changes: 2 additions & 0 deletions layer-api/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
spring:
config:
import: application-secret.properties
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:layer-local-db;DATABASE_TO_UPPER=FALSE;mode=mysql # H2 접속 정보 (전부 소문자로 지정)
Expand Down
14 changes: 14 additions & 0 deletions layer-api/src/test/java/org/layer/LayerApplicationTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.layer;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test")
public class LayerApplicationTests {
@Test
void init(){

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.layer.domain.retrospect.service;

import static org.layer.domain.space.entity.SpaceField.*;

import java.time.LocalDateTime;
import java.util.List;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.layer.domain.member.entity.Member;
import org.layer.domain.member.entity.MemberRole;
import org.layer.domain.member.entity.SocialType;
import org.layer.domain.member.repository.MemberRepository;
import org.layer.domain.question.enums.QuestionType;
import org.layer.domain.retrospect.controller.dto.request.QuestionCreateRequest;
import org.layer.domain.retrospect.controller.dto.request.RetrospectCreateRequest;
import org.layer.domain.retrospect.entity.Retrospect;
import org.layer.domain.retrospect.entity.RetrospectStatus;
import org.layer.domain.retrospect.repository.RetrospectRepository;
import org.layer.domain.space.entity.MemberSpaceRelation;
import org.layer.domain.space.entity.Space;
import org.layer.domain.space.entity.SpaceCategory;
import org.layer.domain.space.repository.MemberSpaceRelationRepository;
import org.layer.domain.space.repository.SpaceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test")
public class RetrospectServiceTest {

@Autowired
private RetrospectService retrospectService;

@Autowired
private RetrospectRepository retrospectRepository;

@Autowired
private MemberRepository memberRepository;

@Autowired
private SpaceRepository spaceRepository;

@Autowired
private MemberSpaceRelationRepository memberSpaceRelationRepository;

@Test
void 정상입력의_경우_회고생성에_성공한다() {
// given
// TODO : 이 부분은 이렇게 처리할지 or data.sql로 처리할지 고민.
Member member = Member.builder()
.name("홍길동")
.email("[email protected]")
.memberRole(MemberRole.USER)
.socialType(SocialType.GOOGLE)
.socialId("123456789")
.build();
Member member1 = memberRepository.saveAndFlush(member);

Space space = new Space(null, SpaceCategory.TEAM, List.of(DEVELOPMENT), "회고스페이스이름입니다", "회고스페이스 설명입니다", 1L, 1L);
spaceRepository.saveAndFlush(space);
memberSpaceRelationRepository.saveAndFlush(new MemberSpaceRelation(member1.getId(), space));

RetrospectCreateRequest request = new RetrospectCreateRequest("회고제목입니다", "회고소개입니다",
List.of(new QuestionCreateRequest("질문1", QuestionType.PLAIN_TEXT.getStyle()),
new QuestionCreateRequest("질문2", QuestionType.PLAIN_TEXT.getStyle()),
new QuestionCreateRequest("질문3", QuestionType.PLAIN_TEXT.getStyle())),
LocalDateTime.of(2024, 8, 4, 3, 5),
false, null, null, null);
// when
Long savedId = retrospectService.createRetrospect(request, 1L, 1L);

// then
Retrospect retrospect = retrospectRepository.findByIdOrThrow(savedId);
Assertions.assertThat(retrospect)
.extracting("title", "introduction", "retrospectStatus")
.containsExactly("회고제목입니다", "회고소개입니다", RetrospectStatus.PROCEEDING);
}
}
12 changes: 12 additions & 0 deletions layer-domain/src/test/java/layer/domain/TestConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package layer.domain;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication(scanBasePackages = {"org.layer"})
@EnableJpaRepositories(basePackages = "org.layer.domain")
@EntityScan(basePackages = "org.layer.domain")
public class TestConfiguration {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package layer.domain.retrospect.entity;

import java.time.LocalDateTime;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.layer.domain.retrospect.entity.Retrospect;
import org.layer.domain.retrospect.entity.RetrospectStatus;

public class RetrospectTest {

@Test
void 진행중인_회고는_진행여부로직에서_예외를_발생시키지_않는다() {
// given
Retrospect retrospect = new Retrospect(1L, "회고제목입니다", "회고소개입니다", RetrospectStatus.PROCEEDING,
LocalDateTime.of(2024, 8, 4, 3, 5));

// when
retrospect.isProceedingRetrospect();

Assertions.assertThat(retrospect.getRetrospectStatus()).isEqualTo(RetrospectStatus.PROCEEDING);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package layer.domain.retrospect.repository;

import java.time.LocalDateTime;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.layer.domain.retrospect.entity.Retrospect;
import org.layer.domain.retrospect.entity.RetrospectStatus;
import org.layer.domain.retrospect.repository.RetrospectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@DataJpaTest
public class RetrospectRepositoryTest {
@Autowired
private RetrospectRepository retrospectRepository;

@Test
void 멀티모듈_레포지토리_테스트() {
// given
Retrospect retrospect = new Retrospect(1L, "회고제목입니다", "회고소개입니다", RetrospectStatus.PROCEEDING,
LocalDateTime.of(2024, 8, 4, 3, 5));

// when
Retrospect saved = retrospectRepository.save(retrospect);

// then
Assertions.assertThat(saved)
.extracting("id", "spaceId", "title", "introduction", "retrospectStatus")
.containsExactly(1L, 1L, "회고제목입니다", "회고소개입니다", RetrospectStatus.PROCEEDING);
}
}

0 comments on commit 467c1b8

Please sign in to comment.