CommencerCommencer gratuitement

Currency conversion

You are working on a currency trading platform. There is a utility class that converts between currencies. Its public convert() method takes an amount in the first currency and an exchange rate, and returns the value in another currency.

Your task is to make the project requirements for it part of the codebase by writing tests. A good place to start is to write a unit test for the success scenario for this method.

Cet exercice fait partie du cours

Introduction to Testing in Java

Afficher le cours

Instructions

  • Call the convert() method with the currency and exchange rate.
  • Use the correct JUnit assertion to check the convertedCurrency amount is equal to the expected one.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

import org.junit.jupiter.api.Test;

import static com.datacamp.util.testing.CustomJUnitTestLauncher.launchTestsAndPrint;
import static org.junit.jupiter.api.Assertions.*;

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.____; 
		
		// Use the correct JUnit assertion, the expected value, and the converted currency amount
        ____(120, ____); 
    }
}
Modifier et exécuter le code