외환: 오류 시나리오
EuropeanCentralBankServer 라이브러리 문서에 따르면, 은행에서 지원하지 않는 통화로 변환을 시도하면 다음과 같은 예외가 발생합니다:
new RuntimeException("Currency not in ECB list: " + currencyName)
이 오류 시나리오를 검증하는 통합 테스트를 작성하세요.
이 연습은 강의의 일부입니다
Java 테스트 소개
연습 안내
- 예외가
RuntimeException의 인스턴스인지 검증하도록 올바른 단언문을 채우세요. - 동등성을 확인하는 올바른 단언문을 사용하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
class Main {
public static void main(String[] args) {
launchTestsAndPrint(ExchangeAppTest.class);
}
}
class ExchangeAppTest {
@Test
void convert_throwsException_whenGetRateThrowsException() {
EuropeanCentralBankServer bank = new EuropeanCentralBankServer();
ExchangeApp exchangeApp = new ExchangeApp(bank);
Exception expectedException = null;
try {
double result = exchangeApp.convertEuroTo("Invalid Currency", 1000);
} catch (Exception e) {
expectedException = e;
}
// Assert that the exception is an instance of the correct class
____(RuntimeException.class, expectedException);
// Assert that the exception message is correct
____("Currency not in ECB list: Invalid Currency", expectedException.getMessage());
}
}