0
0
JUnittesting~10 mins

Why integration tests verify component interaction in JUnit - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test checks if two components work together correctly by verifying their interaction. It ensures that when one component calls another, the expected result is returned.

Test Code - JUnit
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class ServiceA {
    private final ServiceB serviceB;

    ServiceA(ServiceB serviceB) {
        this.serviceB = serviceB;
    }

    String process(String input) {
        String resultFromB = serviceB.transform(input);
        return "Processed: " + resultFromB;
    }
}

class ServiceB {
    String transform(String input) {
        return input.toUpperCase();
    }
}

public class IntegrationTest {

    @Test
    void testServiceAandBInteraction() {
        ServiceB serviceB = new ServiceB();
        ServiceA serviceA = new ServiceA(serviceB);

        String input = "hello";
        String output = serviceA.process(input);

        assertEquals("Processed: HELLO", output);
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startsJUnit test runner initialized-PASS
2Creates instance of ServiceBServiceB object ready to transform strings-PASS
3Creates instance of ServiceA with ServiceB injectedServiceA ready to process input using ServiceB-PASS
4Calls ServiceA.process with input 'hello'ServiceA calls ServiceB.transform internally-PASS
5ServiceB.transform converts 'hello' to 'HELLO'ServiceB returns 'HELLO' to ServiceA-PASS
6ServiceA.process returns 'Processed: HELLO'Output string ready for assertionCheck output equals 'Processed: HELLO'PASS
7Test endsTest passed successfully-PASS
Failure Scenario
Failing Condition: ServiceB.transform returns incorrect result or ServiceA.process does not combine correctly
Execution Trace Quiz - 3 Questions
Test your understanding
What does the integration test verify?
AThat ServiceB alone converts strings to uppercase
BThat ServiceA and ServiceB work together correctly
CThat ServiceA can run without ServiceB
DThat the input string is always lowercase
Key Result
Integration tests confirm that components communicate and work together as expected, catching issues that unit tests alone might miss.