Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Integration Testing | Testing in Development
Java JUnit Library. Types of Testing
course content

Course Content

Java JUnit Library. Types of Testing

Java JUnit Library. Types of Testing

1. Testing in Development
2. Unit Tests
3. Exceptions

bookIntegration Testing

In the previous chapter, we discussed unit testing, where we tested individual modules. However, the program may still not work correctly if we only tested individual modules in isolation. Modules constantly interact with each other; one module is used in another, the second in the third, and so on. Such interactions between modules also need to be tested, and this level of testing is called integration testing.

For example, we have an Engine class with its fields and methods, as well as a Car class in which an instance of the engine class is created using composition. In other words, the methods of the car class involve an object of the engine class. Integration occurs, which means we need to test whether this integration is done correctly.

java

Engine

java

Car

copy
123456789101112131415
public class Engine { private boolean running; public void start() { running = true; } public void stop() { running = false; } public boolean isRunning() { return running; } }

As you can see, we have 2 classes ( you can switch between files above ), each with its own methods. In the Car class, you can see an instance of the Engine class being created. This is called composition. Inside the methods of the Car class, we use the methods of the Engine class, which means that if something goes wrong in the Engine class, the methods in the Car class will also break. So, here is the algorithm of what we need to do:

  • First, we need to test all the methods of the Engine class in isolation;
  • Then, we need to test the methods of the Car class;
  • This testing will be integration testing since the methods of the Car class use the methods of the Engine class;
  • If the integration testing is successful, we perform unit testing of the Car class. (such testing may not be needed at all if the Car class does not have any methods that don't use an instance of the Engine class.)

Within our example, we will only need integration testing to avoid overwhelming you with unnecessary steps. The goal of this chapter is to demonstrate integration testing.

I've highlighted the code blocks for you where testing is performed with comments. As you can see, the test itself is simple and straightforward. There's nothing complex in such testing, and it shows that different modules interact correctly with each other.

A demonstration of a runnable example:

java

main

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
package com.example; public class Main { public static void main(String[] args) { Car car = new Car(); // Testing engine start car.startEngine(); if (car.isEngineRunning()) { System.out.println("Engine start test passed successfully."); } else { System.out.println("Engine start test failed."); } // Testing engine stop car.stopEngine(); if (!car.isEngineRunning()) { System.out.println("Engine stop test passed successfully."); } else { System.out.println("Engine stop test failed."); } } } class Engine { private boolean running; public void start() { running = true; } public void stop() { running = false; } public boolean isRunning() { return running; } } class Car { private Engine engine; public Car() { this.engine = new Engine(); } public void startEngine() { engine.start(); } public void stopEngine() { engine.stop(); } public boolean isEngineRunning() { return engine.isRunning(); } }

Thus, we've performed a basic integration test, demonstrating that these methods work correctly together. Integration testing was successful, and the methods have been tested successfully. Great! We'll delve deeper into integration testing in the third section of this course.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
We're sorry to hear that something went wrong. What happened?
some-alt