Skip to content

Commit

Permalink
feat: @DirtiesContext 제거를 통한 테스트 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
juno-junho committed Jan 7, 2024
1 parent 86a6581 commit e903a27
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.spaceclub.club.repository;

import com.spaceclub.SpaceClubCustomDisplayNameGenerator;
import com.spaceclub.club.domain.ClubUser;
import com.spaceclub.club.domain.ClubUserRole;
import com.spaceclub.user.repository.UserRepository;
import jakarta.persistence.EntityManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.annotation.DirtiesContext;

import java.util.List;
import java.util.Optional;
Expand All @@ -25,17 +24,17 @@
import static org.junit.jupiter.api.Assertions.assertAll;

@DataJpaTest
@DisplayNameGeneration(SpaceClubCustomDisplayNameGenerator.class)
class ClubUserRepositoryTest {

@Autowired
private ClubUserRepository clubUserRepository;

@Autowired
private ClubRepository clubRepository;

@Autowired
private UserRepository userRepository;
@Autowired
private EntityManager entityManager;


@BeforeEach
void setUp() {
Expand All @@ -44,8 +43,13 @@ void setUp() {
clubUserRepository.save(club1User1Manager());
}

@AfterEach
void resetAutoIncrementId() {
entityManager.createNativeQuery("ALTER TABLE CLUB ALTER COLUMN CLUB_ID RESTART WITH 1")
.executeUpdate();
}

@Test
@DirtiesContext
void 클럽_id로_클럽유저_조회에_성공한다() {
// given
clubRepository.save(club2());
Expand All @@ -60,7 +64,6 @@ void setUp() {


@Test
@DirtiesContext
void 클럽의_유저_조회에_성공한다() {
// when
Optional<ClubUser> getClubUser = clubUserRepository.findByClub_IdAndUserId(club1().getId(), user1().getId());
Expand All @@ -74,7 +77,6 @@ void setUp() {
}

@Test
@DirtiesContext
void 클럽의_권한에_따른_인원수_조회에_성공한다() {
// given
userRepository.save(user2());
Expand All @@ -88,7 +90,6 @@ void setUp() {
}

@Test
@DirtiesContext
void 유저에_따른_클럽유저_조회에_성공한다() {
// given
clubRepository.save(club2());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.annotation.DirtiesContext;

import java.util.List;

Expand All @@ -21,17 +20,14 @@
import static com.spaceclub.event.EventTestFixture.showEvent;
import static com.spaceclub.user.UserTestFixture.user1;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.springframework.data.domain.Sort.Direction.DESC;

@DataJpaTest
@DirtiesContext
@DisplayNameGeneration(SpaceClubCustomDisplayNameGenerator.class)
class EventRepositoryTest {

@Autowired
private EventRepository eventRepository;

@Autowired
private ClubRepository clubRepository;

Expand All @@ -47,13 +43,8 @@ class EventRepositoryTest {
List<Event> events = eventPages.getContent();

// then
assertAll(
() -> assertThat(eventPages.getTotalElements())
.isEqualTo(2),
() -> assertThat(events).allSatisfy(event -> {
assertThat(event.getClub()).isEqualTo(club1());
})
);
assertThat(eventPages.getTotalElements()).isEqualTo(2);
events.forEach(event -> assertThat(event.getClub()).isEqualTo(club1()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.spaceclub.user.domain.Provider;
import com.spaceclub.user.domain.User;
import com.spaceclub.user.repository.UserRepository;
import jakarta.persistence.EntityManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.Test;
Expand All @@ -19,7 +21,6 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.annotation.DirtiesContext;

import java.util.List;

Expand All @@ -30,24 +31,29 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.springframework.data.domain.Sort.Direction.DESC;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD;

@DataJpaTest
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
@DisplayNameGeneration(SpaceClubCustomDisplayNameGenerator.class)
class EventUserRepositoryTest {

@Autowired
private EventUserRepository eventUserRepository;

@Autowired
private EventRepository eventRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private ClubRepository clubRepository;
@Autowired
private EntityManager entityManager;

@AfterEach
void resetAutoIncrementId() {
entityManager.createNativeQuery("ALTER TABLE EVENT ALTER COLUMN EVENT_ID RESTART WITH 1")
.executeUpdate();
entityManager.createNativeQuery("ALTER TABLE CLUB ALTER COLUMN CLUB_ID RESTART WITH 1")
.executeUpdate();
}

@BeforeEach
void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
Expand All @@ -20,7 +19,6 @@

@SpringBootTest
@Transactional
@ActiveProfiles("test")
class TemplateNameRepositoryTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.springframework.data.domain.Sort.Direction.DESC;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD;

//SFASDFASD
@DataJpaTest
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
class BookmarkRepositoryTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.annotation.DirtiesContext;

import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -18,7 +17,6 @@
import static org.junit.jupiter.api.Assertions.assertAll;

@DataJpaTest
@DirtiesContext
@DisplayNameGeneration(SpaceClubCustomDisplayNameGenerator.class)
class UserRepositoryTest {

Expand Down

0 comments on commit e903a27

Please sign in to comment.