Why do integration tests focus on verifying the interaction between components rather than testing components individually?
Think about what happens when separate parts of a system connect and exchange data.
Integration tests verify that different components communicate and work together as expected. Unit tests check components alone, but integration tests catch problems in their interaction.
What will be the result of this JUnit integration test that verifies interaction between two components?
class ServiceA { ServiceB b; ServiceA(ServiceB b) { this.b = b; } String callB() { return b.getData(); } } class ServiceB { String getData() { return "data"; } } @Test void testServiceAandBInteraction() { ServiceB b = new ServiceB(); ServiceA a = new ServiceA(b); assertEquals("data", a.callB()); }
Check if ServiceB is properly created and passed to ServiceA.
The test passes because ServiceB is instantiated and injected into ServiceA. The call to ServiceB's method returns "data" as expected.
Which assertion best verifies that two components interact correctly by checking the output of their combined behavior?
Look for an assertion that compares actual output to expected output from the interaction.
Option A directly compares the expected output with the actual output from the interaction, ensuring correctness. Other options check unrelated conditions.
An integration test fails with a NullPointerException when calling a method on a dependent component. What is the most likely cause?
NullPointerException usually means an object reference is null when accessed.
If a dependent component is null, calling its method causes NullPointerException. Proper instantiation or injection is required before use.
Which JUnit 5 feature is best suited to initialize shared components once before all integration tests run to verify component interaction?
Think about initializing expensive resources only once for all tests.
@BeforeAll runs once before all tests and is ideal for setting up shared components to test their interaction efficiently.