Conteúdo do Curso
Java JUnit Library. Types of Testing
Java JUnit Library. Types of Testing
Integration 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.
Engine
Car
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 theEngine
class; - If the integration testing is successful, we perform unit testing of the
Car
class. (such testing may not be needed at all if theCar
class does not have any methods that don't use an instance of theEngine
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:
main
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.
Obrigado pelo seu feedback!