Skip to content
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
115 changes: 115 additions & 0 deletions hiking-api/src/test/java/com/dseoki/api/hiking/HikingServiceTest.java
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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Mock repository에서 예외를 발생시키는 테스트를 추가하시면 좋습니다.


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));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

}