Get startedGet started for free

Integration testing

1. Integration testing

Welcome back! So far, we’ve focused on testing small, individual components. But in object-oriented programming, much of the complexity lies in how components interact and work together.

2. Defining integration testing

Let's continue with some definitions. Integration is the process of combining individual software components or subsystems into a unified system that functions cohesively. This process ensures that different applications or services can communicate and work together effectively. Integration is much like assembling Ikea furniture from its parts. Consequently, An integration test is a test that verifies the interaction between multiple components or modules of an application to ensure they work together correctly. It often involves testing with external dependencies.

3. Dependencies

Finally, let's discuss software dependency. We say that one piece of software (a class, a package, or a service) depends on another if it requires the latter to work properly. This chart provides examples: The user interface depends on the service class, and the service depends on a database and an external server.

4. Integration testing vs. unit testing

Here are some key differences between unit and integration tests. Unit tests check individual components; integration tests check how components work together. When a unit test fails, it's easy to pinpoint where the bug is; when an integration test fails, diagnosing the issue is more complicated; Unit tests are built to be as fast and lightweight as possible; integration tests can be slower and heavier depending on the system they're testing. Unit tests use various tools to isolate the logic under test from its dependencies, while integration tests try to include dependencies. Last, while unit tests have precise and predictable outcomes, integration tests can have less predictable results. Therefore their assertions can be more general.

5. Example: Foreign exchange

Suppose we are building a currency exchange app. Then our app depends on another class that provides exchange rates - the `EuropeanCentralBank` server. A common way to code this dependency is to have a `EuropeanCentralBankServer` object as a member variable which is passed through the EchangeApp's constructor. In the method `convertEuroTo()` we call `getRateEuroTo()` on that object to get real currency rates and use them in conversions.

6. Integration testing foreign exchange

If we test this behavior, we are testing both `ExchangeApp`, the `EuropeanCentralBankServer`, and their interactions. This is because our test is calling the `ExchangeApp` class, and `ExchangeApp` calls a method from `EuropeanCentralBankServer`. In this example test we create a EuropeanCentralBankServer object and use it to create an ExchangeApp. Then we assert that we can convert Euro to USD. Now, because currency rates are unpredictable, we cannot assert on the exact value of the outcome. But we can still sanity test that we don't get zero or negative amounts of money, and that the conversion completes without throwing exceptions.

7. Let's practice!

Let's see these dependencies in action and test them!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.