Skip to content

Commit

Permalink
test : S3 이미지 업로드 성공 테스트 코드 작성
Browse files Browse the repository at this point in the history
(CC-132)
  • Loading branch information
j2noo committed Aug 11, 2024
1 parent 70a42b3 commit f18e1e7
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import ai.softeer.caecae.adminfindinggame.domain.exception.AdminFindingGameException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.PutObjectRequest;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ResourceUtils;

import java.io.File;
Expand All @@ -22,6 +25,9 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

@ExtendWith(MockitoExtension.class)
//@TestPropertySource(properties = {
// "spring.s3.bucket=caecae"
//})
class S3ServiceTest {

@Mock
Expand All @@ -32,9 +38,6 @@ class S3ServiceTest {

private MockMultipartFile mockMultipartFile;

@Value("${spring.s3.bucket}")
private String bucket;


@BeforeEach
void setUp() throws IOException {
Expand All @@ -59,14 +62,14 @@ void setUp() throws IOException {
});
}

// TODO : 테스트코드 환경변수를 사용하도록 설정
@Disabled
@Test
@DisplayName("S3 이미지 업로드 성공")
void uploadFile_성공() throws IOException {
ReflectionTestUtils.setField(s3Service, "bucket", "test-bucket-name");

// 성공할 때 반환되는 URL을 모킹
String expectedUrl = "https://s3.ap-south-1.amazonaws.com/your-bucket-name/caecae-some-uuid.png";
Mockito.when(amazonS3.getUrl(bucket, Mockito.anyString())).thenReturn(new URL(expectedUrl));
Mockito.when(amazonS3.getUrl(Mockito.anyString(), Mockito.anyString())).thenReturn(new URL(expectedUrl));

//when
S3ResponseDto response = s3Service.uploadFile(mockMultipartFile, "answer");
Expand Down

0 comments on commit f18e1e7

Please sign in to comment.