Skip to content

Commit

Permalink
test: ForecastApiClient 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
Sehee-Lee-01 committed Jan 6, 2024
1 parent 5de1c6e commit 0219de6
Showing 1 changed file with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.backendoori.ootw.weather.util.client;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.time.LocalDateTime;
import java.util.NoSuchElementException;
import java.util.stream.Stream;
import com.backendoori.ootw.weather.dto.forecast.BaseDateTime;
import com.backendoori.ootw.weather.util.BaseDateTimeCalculator;
import net.datafaker.Faker;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@TestInstance(Lifecycle.PER_CLASS)
class ForecastApiClientTest {

static final Integer VALID_NX = 50;
static final Integer VALID_NY = 127;
static final BaseDateTime TEMP_BASE_DATETIME = BaseDateTimeCalculator.getRequestBaseDateTime(LocalDateTime.now());
static Faker faker = new Faker();

@Autowired
ForecastApi forecastApi;
@Autowired
ForecastApiClient forecastApiClient;


static Stream<Arguments> provideInvalidRange() {
return Stream.of(
Arguments.of(faker.number().negative(), VALID_NY),
Arguments.of(VALID_NX, faker.number().negative()),
Arguments.of(faker.number().numberBetween(1000, 10000), VALID_NY),
Arguments.of(VALID_NX, faker.number().numberBetween(1000, 10000)));
}

@Test
@DisplayName("정보가 없는 위치 값으로 현재 날씨 불러오기에 실패한다.")
void requestUltraShortForecastItemsFailByNoSuchElementException() {
// given
Integer nx = 0;
Integer ny = 0;

// when // then
assertThrows(NoSuchElementException.class,
() -> forecastApiClient.requestUltraShortForecastItems(TEMP_BASE_DATETIME, nx, ny));
}

@ParameterizedTest
@MethodSource("provideInvalidRange")
@DisplayName("유효하지 않은 파라미터 범위로 현재 날씨 불러오기에 실패한다.")
void requestUltraShortForecastItemsFailByIllegalRange(Integer nx, Integer ny) {
// given // when // then
assertThrows(IllegalArgumentException.class,
() -> forecastApiClient.requestUltraShortForecastItems(TEMP_BASE_DATETIME, nx, ny));
}

}

0 comments on commit 0219de6

Please sign in to comment.