Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OMCT-347] 아이템 추가 통합 테스트 #175

Merged
merged 9 commits into from
Jan 8, 2024
7 changes: 4 additions & 3 deletions bucketback-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ dependencies {
implementation project(':bucketback-common')
implementation project(':bucketback-infrastructure')

testImplementation(testFixtures(project(':bucketback-domain')))
testImplementation 'org.springframework.boot:spring-boot-starter-test'

//swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'

// jwt
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'

// 테스트 관련
testImplementation(testFixtures(project(':bucketback-domain')))
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

jar {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.programmers.bucketback;

import org.junit.jupiter.api.Disabled;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;

@Disabled
@SpringBootTest
@ActiveProfiles("test")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5;
activeProfile을 사용하지 않고 test 하위 resource 폴더에 테스트만을 위한 yml을 두는 건 어떤가용 ?? 이거는 다같이 회의해보면 좋을 것 같아요!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

active profile을 사용할 경우 로그 설정, 관련 빈 설정 등 환경 관리를 할 수 있어서 적용하였습니다.

@Sql(scripts = {"classpath:truncate.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
Comment on lines +8 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 이게 통합테스트를 위한 세팅인가요??!
혹시 disabled와 sql 어노테이션 왜, 어떻게 사용하는 건지 설명 부탁드려도 될까요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sql 어노테이션은 테스트 메서드가 실행되기 전에 truncate.sql을 실행하기 위해을 사용하였습니다!
Disabled 어노테이션은 IntegrationTest 클래스는 테스트 하지 않은 클래스이기 때문에 테스트를 하지 않도록 만들기 위해 추가 하였습니다!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저 부분은 다같이 다양한 방법을 찾아보고 회의해서 결정하는 것은 어떤가요!?

public class IntegrationTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.programmers.bucketback.domains.item.application;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import com.programmers.bucketback.IntegrationTest;
import com.programmers.bucketback.common.model.ItemIdRegistry;
import com.programmers.bucketback.common.model.ItemIdRegistryBuilder;
import com.programmers.bucketback.domains.item.application.dto.ItemAddServiceResponse;
import com.programmers.bucketback.domains.item.domain.setup.ItemSetup;
import com.programmers.bucketback.domains.item.implementation.MemberItemValidator;
import com.programmers.bucketback.global.util.MemberUtils;

class ItemServiceTest extends IntegrationTest {

@Autowired
private ItemSetup itemSetup;

@Autowired
private ItemService itemService;

@MockBean
private MemberUtils memberUtils;

@MockBean
private MemberItemValidator memberItemValidator;

@Test
@DisplayName("나의 아이템을 추가한다.")
public void addItemTest() {
// given
given(memberUtils.getCurrentMemberId()).willReturn(1L);

// memberSetup이 만들어지면 변경할 예정
doNothing().when(memberItemValidator).validateExistMemberItem(any(), any());

ItemIdRegistry itemIdRegistry = ItemIdRegistryBuilder.build();
ItemAddServiceResponse expectedResponse = new ItemAddServiceResponse(itemIdRegistry.itemIds());

itemIdRegistry.itemIds()
.forEach(itemId -> itemSetup.saveOne(itemId));

// when
ItemAddServiceResponse actualResponse = itemService.addItem(itemIdRegistry);

// then
assertThat(actualResponse)
.usingRecursiveComparison()
.isEqualTo(expectedResponse);
}
}
18 changes: 18 additions & 0 deletions bucketback-api/src/test/resources/truncate.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
set
FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE bucket_items;
TRUNCATE TABLE feed_items;
TRUNCATE TABLE feed_likes;
TRUNCATE TABLE inventory_items;
TRUNCATE TABLE members_items;
TRUNCATE TABLE voters;
TRUNCATE TABLE buckets;
TRUNCATE TABLE comments;
TRUNCATE TABLE feeds;
TRUNCATE TABLE inventories;
TRUNCATE TABLE reviews;
TRUNCATE TABLE votes;
TRUNCATE TABLE items;
TRUNCATE TABLE members;
set
FOREIGN_KEY_CHECKS = 1;
3 changes: 3 additions & 0 deletions bucketback-domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ dependencies {
testFixturesCompileOnly 'org.projectlombok:lombok'
testFixturesAnnotationProcessor 'org.projectlombok:lombok'

// 테스트 setup을 위한 의존성 추가
testFixturesCompileOnly 'org.springframework.boot:spring-boot-starter-data-jpa'

// querydsl 설정 추가
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.programmers.bucketback.domains.item.domain.setup;

import org.springframework.stereotype.Component;

import com.programmers.bucketback.domains.item.domain.Item;
import com.programmers.bucketback.domains.item.domain.ItemBuilder;
import com.programmers.bucketback.domains.item.repository.ItemRepository;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class ItemSetup {

private final ItemRepository itemRepository;

public Item saveOne(final Long itemId) {
Item item = ItemBuilder.build(itemId);

return itemRepository.save(item);
}
}
Loading