examples for integration testing

Mastering Integration Testing: 10 Practical Examples

By William White
AD

Integration testing is a critical phase in the software development lifecycle, ensuring that different modules or components of a software system work together seamlessly. It helps identify gaps and issues that might not be evident during unit testing. Let's dive into some practical examples to illustrate this crucial testing phase.

Before we delve into examples, it's essential to understand that integration tests often use a testing framework like Jest, Mocha, or PyTest to automate the test cases. They also rely on test doubles (mock, stub, fake, or dummy) to isolate the system under test from its dependencies.

Testing Multiple Modules

One of the primary goals of integration testing is to validate the interactions between modules. Let's consider a simple application with 'Auth' and 'User' modules in Node.js using Jest and Mockgen.

AD
Software testing
Software testing
Milan Jovanović on LinkedIn: Are you writing integration tests? Integration tests verify your system… | 26 comments
Milan Jovanović on LinkedIn: Are you writing integration tests? Integration tests verify your system… | 26 comments
Unit Test vs Integration Test: Know the Difference! 🧪🏗️
Unit Test vs Integration Test: Know the Difference! 🧪🏗️

The 'Auth' module exports a login function that takes a username and password. The 'User' module depends on this 'Auth' module to authenticate users. Here's how we can integration test these modules:

Testing 'Auth' and 'User' modules together

First, we'll mock the 'User' module to control its behavior and isolate the 'Auth' module:

Milan Jovanović on LinkedIn: Using in-memory databases for integration testing is a waste of time.… | 30 comments
Milan Jovanović on LinkedIn: Using in-memory databases for integration testing is a waste of time.… | 30 comments
System Vs Integration Testing
System Vs Integration Testing
10 Best Integration Testing Tools for Integration Tests in 2025
10 Best Integration Testing Tools for Integration Tests in 2025
```javascript jest.mock('../user', () => ({ isUserVerified: jest.fn() })); ```

Then, we'll write an integration test to confirm that both modules work together:

```javascript it('should log in a verified user', () => { const mockUser = { username: 'test', password: 'test', isVerified: true }; const userModule = require('../user'); const authModule = require('./auth'); // Mock the response from the 'User' module userModule.isUserVerified.mockReturnValue(mockUser.isVerified); // Call the 'Auth' module's login function const response = authModule.login(mockUser.username, mockUser.password); // Assert the expected behavior expect(response).toBe(true); }); ```

Testing multiple interconnected modules

Integration tests can validate complex dependencies. Suppose we have an 'Email' module that depends on the successful authentication from the 'Auth' and 'User' modules to send an email:

AD

We can write an integration test to ensure all three modules work together seamlessly:

✨ Differentiation & Integration Short Notes ✨ Easy formulas, shortcuts, tricks, and quick revision
✨ Differentiation & Integration Short Notes ✨ Easy formulas, shortcuts, tricks, and quick revision
several devices connected to each other with icons coming out of them
several devices connected to each other with icons coming out of them
Integration Testing
Integration Testing
System Integration Testing (SIT): Best Practices and Examples
System Integration Testing (SIT): Best Practices and Examples
an info sheet describing how to use the internet
an info sheet describing how to use the internet
the manual for testing and testing boxes with instructions on how to use them in an application
the manual for testing and testing boxes with instructions on how to use them in an application
```javascript it('should send an email to a verified user', () => { // ... (mock 'User' and 'Auth' modules as before) const emailModule = require('../email'); const mockEmail = { to: 'test@test.com', body: ' Welcome!' }; // Mock the 'Email' module's send function emailModule.send.mockReturnValue(Promise.resolve()); // Call the 'Auth' module's login function and expect it to trigger the 'Email' module's send function return authModule.login(mockUser.username, mockUser.password).then(() => { expect(emailModule.send).toHaveBeenCalledWith(mockEmail); }); }); ```

Testing APIs

Integration tests are also crucial for validating APIs. They can help ensure that API endpoints return expected results when called with valid inputs and throw appropriate errors with invalid inputs. Let's consider a simple REST API using Express and Supertest:

Assume we have a '/users' POST endpoint that creates a new user. Here's how we can integration test this endpoint:

AD

Testing a POST API endpoint

First, we'll stub the API using Express and define the '/users' POST endpoint and its expected behavior:

```javascript const express = require('express'); const request = require('supertest'); const app = express(); app.use(express.json()); app.post('/users', (req, res) => { const { username, password } = req.body; // ... (validate and create the user) res.status(201).json({ message: 'User created successfully' }); }); ```

Then, we'll write an integration test using Supertest to validate this API endpoint:

```javascript describe('POST /users', () => { it('should create a new user', async () => { const user = { username: 'test', password: 'test' }; const response = await request(app).post('/users').send(user); // Assert the expected response status and body expect(response.statusCode).toBe(201); expect(response.body).toEqual({ message: 'User created successfully' }); }); it('should return 400 for invalid user data', async () => { const user = { username: 'test' }; // Invalid, missing 'password' field const response = await request(app).post('/users').send(user); // Assert the expected response status and body expect(response.statusCode).toBe(400); expect(response.body).toEqual({ error: 'Invalid user data' }); }); }); ```

The final test validates that the API handles invalid user data by returning an appropriate error message and status code.

AD

Integration testing plays a pivotal role in ensuring the reliability and correctness of software systems. By catching issues early in the development process, it saves time and resources in the long run. Thus, investing in robust integration testing strategies is crucial for building high-quality software.

AD