0
0
JUnittesting~20 mins

Why different doubles serve different purposes in JUnit - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Test Doubles
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of a Mock in Unit Testing

In JUnit testing, what is the main purpose of using a mock double?

ATo simulate the behavior of real objects and verify interactions with them
BTo replace the entire system under test with a fake implementation
CTo store test data for later use in assertions
DTo generate random input data automatically during tests
Attempts:
2 left
💡 Hint

Think about how mocks help check if certain methods were called.

🧠 Conceptual
intermediate
2:00remaining
Difference Between Stub and Fake

Which statement best describes the difference between a stub and a fake test double?

AA stub records interactions, while a fake only returns null values
BA stub provides predefined responses, while a fake has a working implementation but is simpler than the real object
CA stub is used for integration tests, while a fake is used only in UI tests
DA stub is a real object, while a fake is a mock object
Attempts:
2 left
💡 Hint

Consider which double has actual logic and which just returns fixed data.

Predict Output
advanced
2:00remaining
JUnit Mock Verification Output

What will be the output of the following JUnit test using Mockito when the verification fails?

JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;

class ServiceTest {
    @Test
    void testServiceCall() {
        Service mockService = mock(Service.class);
        // No method call on mockService
        verify(mockService).performAction();
    }
}

interface Service {
    void performAction();
}
ATest passes with no output
BNullPointerException at verify line
Corg.mockito.exceptions.verification.junit.ArgumentsAreDifferent: Wanted but not invoked: performAction()
DCompilation error due to missing method implementation
Attempts:
2 left
💡 Hint

Think about what happens if you verify a method call that never happened.

assertion
advanced
2:00remaining
Correct Assertion for Stubbed Method Return

Given a stubbed method in JUnit using Mockito that returns a fixed value, which assertion correctly verifies the returned value?

JUnit
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class CalculatorTest {
    @Test
    void testAdd() {
        Calculator calc = mock(Calculator.class);
        when(calc.add(2, 3)).thenReturn(5);
        int result = calc.add(2, 3);
        // Which assertion is correct here?
    }
}

interface Calculator {
    int add(int a, int b);
}
AassertFalse(result == 5);
BassertTrue(result == 6);
CassertNull(result);
DassertEquals(5, result);
Attempts:
2 left
💡 Hint

Check the expected value from the stubbed method.

framework
expert
3:00remaining
Choosing the Right Test Double for Integration Testing

In a complex integration test involving a database and external API, which test double is most appropriate to isolate the external API while keeping realistic behavior for the database?

AUse a fake for the external API and real database connection
BUse a stub for the database and a mock for the external API
CUse a mock for the database and a stub for the external API
DUse a mock for both the database and external API
Attempts:
2 left
💡 Hint

Consider which double provides simple working behavior and which verifies interactions.