Introduction to Testing in Java
Maria Milusheva
Senior Software Engineer
Unit tests:
Integration tests:
Suppose we are building a currency exchange app:
public class ExchangeApp { private EuropeanCentralBankServer bank; // Enables ExchangeApp to use the bank server public ExchangeApp(EuropeanCentralBankServer bank) { this.bank = bank; // Save the object passed through the constructor }
public double convertEuroTo(String currency, double amount) { double rate = this.bank.getRateEuroTo(currency); return amount * rate; // Use the return values of the bank method in calculations } }
Integration testing verifies both the convertEuroTo
method and EuropeanCentralBankServer
:
@Test void convert_convertsWithoutError() { EuropeanCentralBankServer bank = new EuropeanCentralBankServer(); ExchangeApp exchangeApp = new ExchangeApp(bank); // Pass bank object to constructor
double amount = 1000.0; String currency = "USD"; // convertEuroTo calls getRateEuroTo from the bank object double result = exchangeApp.convertEuroTo(currency, amount); assertTrue(result > 0); // Can't predict exact value, can only sanity test }
Introduction to Testing in Java