- 
                Notifications
    You must be signed in to change notification settings 
- Fork 105
[5기 최정은] Shorten-URL 과제 제출합니다. #68
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
base: JeongeunChoi
Are you sure you want to change the base?
Changes from 1 commit
d7def34
              14f5576
              bb9a605
              37dc9c3
              06f74a8
              a59b5e4
              6cf971e
              be2d0e5
              4e624a2
              fee5af2
              2f35454
              bea61ca
              f510a9a
              c95d11f
              4f037a5
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package com.springboot.springbooturlshortner.service; | ||
|  | ||
| import com.springboot.springbooturlshortner.domain.Url; | ||
| import com.springboot.springbooturlshortner.exception.UrlException; | ||
| import com.springboot.springbooturlshortner.exception.UrlExceptionCode; | ||
| import com.springboot.springbooturlshortner.repository.UrlRepository; | ||
| import com.springboot.springbooturlshortner.util.Base62Util; | ||
| 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.junit.jupiter.MockitoExtension; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
|  | ||
| import java.util.Optional; | ||
|  | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.when; | ||
|  | ||
| @ExtendWith(MockitoExtension.class) | ||
| class UrlServiceTest { | ||
|  | ||
| @InjectMocks | ||
| private UrlService urlService; | ||
| @InjectMocks | ||
| private Base62Util base62Util; | ||
| @Mock | ||
| private UrlRepository urlRepository; | ||
| @Value("${spring.start-url}") | ||
| private String startUrl; | ||
|  | ||
| @BeforeEach | ||
| void setUp() { | ||
| urlService = new UrlService(urlRepository, base62Util); | ||
| } | ||
| 
      Comment on lines
    
      +35
     to 
      +38
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 테스트마다 초기화되어야하는 대상일까요? | ||
|  | ||
| @Test | ||
| @DisplayName("단축 url 생성 성공") | ||
| void Success_CreateShortenUrl() { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드명이 대문자로 되어있어요. | ||
| // given | ||
| String originUrl = "https://test-url"; | ||
| ShortenUrlRequestDto shortenUrlRequestDto = new ShortenUrlRequestDto(originUrl); | ||
| Url url = shortenUrlRequestDto.toUrlEntity(); | ||
| url.setId(10000L); | ||
| String expectedShortenUrl = startUrl + "/" + base62Util.encoding(url.getId()); | ||
| when(urlRepository.save(any(Url.class))).thenReturn(url); | ||
|  | ||
| // when | ||
| String resultShortenUrl = urlService.createShortenUrl(shortenUrlRequestDto); | ||
|  | ||
| // then | ||
| Assertions.assertEquals(expectedShortenUrl, resultShortenUrl); | ||
| } | ||
|  | ||
| @Test | ||
| @DisplayName("단축 url 생성 실패 - 유효하지 않은 기존 url") | ||
| void Fail_CreateShortenUrl_With_InvalidOriginUrl() { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드명이 대문자로 되어있어요. | ||
| // given | ||
| String originUrl = "htt://test-url"; | ||
| ShortenUrlRequestDto shortenUrlRequestDto = new ShortenUrlRequestDto(originUrl); | ||
|  | ||
| // when | ||
| UrlException e = Assertions.assertThrows(UrlException.class, () -> urlService.createShortenUrl(shortenUrlRequestDto)); | ||
|  | ||
| // then | ||
| Assertions.assertEquals(UrlExceptionCode.INVALID_ORIGIN_URL.getMessage(), e.getMessage()); | ||
| } | ||
|  | ||
| @Test | ||
| @DisplayName("기존 url 반환 성공") | ||
| void Success_GetOriginUrl() { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드명이 대문자로 되어있어요. | ||
| // given | ||
| String expectedOriginUrl = "https://test-url"; | ||
| ShortenUrlRequestDto shortenUrlRequestDto = new ShortenUrlRequestDto(expectedOriginUrl); | ||
| Url url = shortenUrlRequestDto.toUrlEntity(); | ||
| url.setId(10000L); | ||
| String shortenUrl = startUrl + "/" + base62Util.encoding(url.getId()); | ||
| when(urlRepository.findById(any(Long.class))).thenReturn(Optional.of(url)); | ||
|  | ||
| // when | ||
| String resultOriginUrl = urlService.getOriginUrl(shortenUrl); | ||
|  | ||
| // then | ||
| Assertions.assertEquals(expectedOriginUrl, resultOriginUrl); | ||
| } | ||
|  | ||
| @Test | ||
| @DisplayName("단축 url 생성 실패 - 존재하지 않는 단축 url") | ||
| void Fail_GetOriginUrl_With_NotExistingShortenUrl() { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메서드명이 대문자로 되어있어요. | ||
| // given | ||
| String expectedOriginUrl = "https://test-url"; | ||
| ShortenUrlRequestDto shortenUrlRequestDto = new ShortenUrlRequestDto(expectedOriginUrl); | ||
| Url url = shortenUrlRequestDto.toUrlEntity(); | ||
| url.setId(10000L); | ||
| String shortenUrl = startUrl + "/" + base62Util.encoding(url.getId()) + "test"; | ||
| when(urlRepository.findById(any(Long.class))).thenReturn(Optional.empty()); | ||
|  | ||
| // when | ||
| UrlException e = Assertions.assertThrows(UrlException.class, () -> urlService.getOriginUrl(shortenUrl)); | ||
|  | ||
| // then | ||
| Assertions.assertEquals(UrlExceptionCode.NOT_FOUND.getMessage(), e.getMessage()); | ||
| } | ||
|  | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InjectMocks를 사용하는게 맞을까요?