货币转换
您正在开发一款货币交易平台。这里有一个用于不同货币间转换的工具类。它的公共方法 convert() 接收第一种货币中的金额和汇率,并返回换算成另一种货币的数值。
您的任务是通过编写测试,把该方法的项目需求纳入代码库。一个合适的起点是为该方法的成功场景编写单元测试。
所需的 JUnit 包已为您导入。
本练习是课程的一部分
Java 测试入门
练习说明
- 使用货币金额和汇率调用
convert()方法。 - 使用正确的 JUnit 断言,检查
convertedCurrency的数值是否等于期望值。
交互式实操练习
通过完成这段示例代码来试试这个练习。
public class CurrencyConverterWithTests {
public static void main(String[] args) {
launchTestsAndPrint(CurrencyConverterTest.class);
}
}
class CurrencyConverter {
public static double convert(double currency, double exchangeRate) {
return currency * exchangeRate;
}
}
class CurrencyConverterTest {
@Test
void convert_returnsConvertedCurrency_whenValidInputs() {
double currency = 100;
double exchangeRate = 1.2;
// Call the convert method with the currency and exchange rate
double convertedCurrency = CurrencyConverter.____(currency, ____);
// Use the correct JUnit assertion, the expected value, and the converted currency amount
____(120, ____);
}
}