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

[5기 최정은] Shorten-URL 과제 제출합니다. #68

Open
wants to merge 15 commits into
base: JeongeunChoi
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

테스트마다 초기화되어야하는 대상일까요?


@Test
@DisplayName("단축 url 생성 성공")
void Success_CreateShortenUrl() {

Choose a reason for hiding this comment

The 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() {

Choose a reason for hiding this comment

The 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() {

Choose a reason for hiding this comment

The 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() {

Choose a reason for hiding this comment

The 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());
}

}