Skip to content

Commit

Permalink
TEST(project) :: ProjectRepositoryTest 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
Woongbin06 committed Oct 29, 2024
1 parent 47b870a commit 2f0c284
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.woongeya.zoing.project.domain;
package com.woongeya.zoing.domain.project.domain;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import com.woongeya.zoing.domain.project.domain.Project;
import com.woongeya.zoing.domain.project.domain.type.ProjectState;

public class ProjectTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.woongeya.zoing.domain.project.domain.fixture;

import java.time.LocalDate;

import com.woongeya.zoing.domain.project.domain.Project;
import com.woongeya.zoing.domain.project.domain.type.ProjectState;

public class ProjectFixture {

public static Project 프로젝트() {
return Project.builder()
.name("쪼잉")
.content("쪼잉으로 오세용")
.imgUrl("iamge")
.viewCount(0L)
.state(ProjectState.FINDING)
.requiredPeople(4)
.currentPeople(1)
.endDate(LocalDate.now())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.woongeya.zoing.domain.project.domain.repository;

import static com.woongeya.zoing.domain.project.domain.fixture.ProjectFixture.*;
import static org.assertj.core.api.Assertions.*;

import java.util.List;
import java.util.NoSuchElementException;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.woongeya.zoing.domain.project.domain.Project;
import com.woongeya.zoing.global.repository.RepositoryTest;

public class ProjectRepositoryTest extends RepositoryTest {

@Autowired
private ProjectRepository projectRepository;

@Test
void 모든_프로젝트를_조회할_수_있다() {
// given
Project 저장된_프로젝트1 = 프로젝트_저장(프로젝트());
Project 저장된_프로젝트2 = 프로젝트_저장(프로젝트());

// when
List<Project> 조회된_프로젝트들 = projectRepository.findAll();

// then
assertThat(조회된_프로젝트들).contains(저장된_프로젝트1, 저장된_프로젝트2);
}

@Test
void 아이디로_프로젝트를_조회할_수_있다() {
// given
Project 저장된_프로젝트 = 프로젝트_저장(프로젝트());
Long 아이디 = 저장된_프로젝트.getId();

// when
Project 조회된_프로젝트 = projectRepository.findById(아이디).get();

// then
assertThat(조회된_프로젝트).isEqualTo(저장된_프로젝트);
}

@Test
void 프로젝트를_삭제할_수_있다() {
// given
Project 저장된_프로젝트 = 프로젝트_저장(프로젝트());
Long 아이디 = 저장된_프로젝트.getId();

// when
projectRepository.delete(저장된_프로젝트);

// then
assertThatThrownBy(() -> projectRepository.findById(아이디).get())
.isInstanceOf(NoSuchElementException.class);
}

private Project 프로젝트_저장(Project project) {
return projectRepository.save(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.woongeya.zoing.global.config;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;

import com.querydsl.jpa.impl.JPAQueryFactory;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

@TestConfiguration
public class QueryDslTestConfig {

@PersistenceContext
private EntityManager entityManager;

@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.woongeya.zoing.global.repository;

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

import com.woongeya.zoing.global.config.JpaConfig;
import com.woongeya.zoing.global.config.QueryDslTestConfig;

@DataJpaTest
@Import({JpaConfig.class, QueryDslTestConfig.class})
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public abstract class RepositoryTest {
}
4 changes: 2 additions & 2 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ spring:

jpa:
database: mysql
database-platform: org.hibernate.dialect.MySQL8Dialect
database-platform: org.hibernate.dialect.H2Dialect
generate-ddl: true
hibernate:
ddl-auto: update
ddl-auto: create
properties:
hibernate:
format_sql: true
Expand Down

0 comments on commit 2f0c284

Please sign in to comment.