-
Notifications
You must be signed in to change notification settings - Fork 1
tdd mock test #13
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
Open
dseoki
wants to merge
2
commits into
main
Choose a base branch
from
feature/tdd-first
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
tdd mock test #13
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
hiking-api/src/test/java/com/dseoki/api/hiking/HikingServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package com.dseoki.api.hiking; | ||
|
|
||
| import com.dseoki.api.entity.Hiking; | ||
| import com.dseoki.api.hiking.domain.HikingDto; | ||
| import com.dseoki.api.hiking.domain.HikingResponse; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.core.io.ResourceLoader; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.security.SecureRandom; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Arrays; | ||
| import java.util.Base64; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| @SpringBootTest | ||
| public class HikingServiceTest { | ||
|
|
||
| private final ResourceLoader resourceLoader; | ||
|
|
||
| HikingRepository repository = mock(HikingRepository.class); | ||
| HikingService hikingService = new HikingService(repository); | ||
|
|
||
| @Autowired | ||
| public HikingServiceTest(ResourceLoader resourceLoader) { | ||
| this.resourceLoader = resourceLoader; | ||
| } | ||
|
|
||
| @Test | ||
| public void searchHikingTest() { | ||
| Hiking hiking1 = new Hiking(); | ||
| hiking1.setId(1L); | ||
| hiking1.setHikeStartDate(LocalDateTime.of(2024, 2, 15, 9, 00)); | ||
|
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. 반복되어 사용되는 LocalDateTime.of(2024, 2, 15, 9, 00)와 같은 값은 상수로 처리하시면 좋을 것 같습니다. |
||
| hiking1.setHikeEndDate(LocalDateTime.of(2024, 2, 15, 9, 30)); | ||
|
|
||
| Hiking hiking2 = new Hiking(); | ||
| hiking2.setId(2L); | ||
| hiking2.setHikeStartDate(LocalDateTime.of(2024, 5, 15, 9, 00)); | ||
| hiking2.setHikeEndDate(LocalDateTime.of(2024, 5, 15, 9, 30)); | ||
|
|
||
| when(repository.findAll()).thenReturn(Arrays.asList(hiking1, hiking2)); | ||
|
|
||
| List<HikingResponse> result = hikingService.searchHiking(); | ||
|
|
||
| assertEquals(2, result.size()); | ||
| assertEquals(1L, result.get(0).getId()); | ||
| } | ||
|
|
||
| @Test | ||
| public void insertHikingTest() { | ||
| HikingDto hikingDto = new HikingDto(); | ||
| hikingDto.setTitle("Hiking test"); | ||
| hikingDto.setHikeStartDate("2021-02-13 09:00"); | ||
| hikingDto.setHikeEndDate("2021-02-13 18:00"); | ||
| hikingDto.setDistance(10); | ||
| hikingDto.setElevation(10); | ||
| hikingDto.setEstimatedDuration(10); | ||
| hikingDto.setDescription("mock tdd test description..."); | ||
|
|
||
| // 테스트 실행 | ||
| hikingService.insertHiking(hikingDto); | ||
|
|
||
| // Mock 리포지토리의 save 메서드가 호출되었는지 확인 | ||
| verify(repository, times(1)).save(any(Hiking.class)); | ||
| } | ||
|
|
||
| // @Test | ||
|
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. 주석처리된 코드는 지워주시는게 좋습니다. |
||
| // public void filePathText() throws IOException { | ||
| // final String current_date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); | ||
| //// final String path = "../images/" + current_date; | ||
| //// | ||
| //// File file = new File(""); | ||
| //// System.out.println("================== file path: " + file.getPath()); | ||
| // System.out.println("================== file path: " + new File("images/" + current_date).getPath()); | ||
| // System.out.println("================== file absolute path: " + new File("images/" + current_date).getAbsolutePath()); | ||
| //// System.out.println("================== file canonical path: " + file.getCanonicalPath()); | ||
| //// | ||
| //// if (!file.exists()) { | ||
| //// file.mkdirs(); | ||
| //// } | ||
| //// System.out.println("file exists:" + file.exists()); | ||
| // } | ||
|
|
||
| // @Test | ||
| // public void secretKey() { | ||
| // byte[] randaomBytes = new byte[64]; | ||
| // new SecureRandom().nextBytes(randaomBytes); | ||
| // | ||
| // String encodedKey = Base64.getEncoder().encodeToString(randaomBytes); | ||
| // System.out.println("Base64 Encoded Secret Key: " + encodedKey); | ||
| // | ||
| // | ||
| // SecureRandom random = new SecureRandom(); | ||
| // byte[] bytes = new byte[64]; | ||
| // random.nextBytes(bytes); | ||
| // StringBuilder hexString = new StringBuilder(); | ||
| // for (byte b : bytes) { | ||
| // String hex = Integer.toHexString(0xff & b); | ||
| // if (hex.length() == 1) { | ||
| // hexString.append('0'); | ||
| // } | ||
| // hexString.append(hex); | ||
| // } | ||
| // System.out.println(hexString.toString()); | ||
| // } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Mock repository에서 예외를 발생시키는 테스트를 추가하시면 좋습니다.