0
0
JUnittesting~20 mins

Choosing the right test double in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Test Double Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Test Doubles: Which to Use?

You want to isolate a class under test by replacing its dependency with a test double that records interactions but does not implement any real logic. Which test double should you use?

AStub
BFake
CSpy
DMock
Attempts:
2 left
💡 Hint

Think about which test double lets you verify method calls after the test runs.

Predict Output
intermediate
2:00remaining
Output of JUnit Test Using a Stub

What is the output of the following JUnit test when run?

JUnit
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

public class PaymentProcessorTest {
    interface PaymentGateway {
        boolean charge(double amount);
    }

    static class PaymentGatewayStub implements PaymentGateway {
        public boolean charge(double amount) {
            return true; // always succeeds
        }
    }

    static class PaymentProcessor {
        private PaymentGateway gateway;

        public PaymentProcessor(PaymentGateway gateway) {
            this.gateway = gateway;
        }

        public boolean process(double amount) {
            return gateway.charge(amount);
        }
    }

    @Test
    void testProcessPayment() {
        PaymentGateway stub = new PaymentGatewayStub();
        PaymentProcessor processor = new PaymentProcessor(stub);
        boolean result = processor.process(100.0);
        assertTrue(result);
    }
}
ATest does not compile
BTest fails due to NullPointerException
CTest fails due to assertion error
DTest passes successfully
Attempts:
2 left
💡 Hint

The stub always returns true for charge, so process should succeed.

assertion
advanced
2:00remaining
Correct Assertion for Mock Verification

Given a mock object mockService in JUnit with Mockito, which assertion correctly verifies that process() was called exactly once?

JUnit
mockService.process();
Averify(mockService, times(1)).process();
BassertEquals(1, mockService.process());
Cverify(mockService).process(1);
DassertTrue(mockService.process.calledOnce());
Attempts:
2 left
💡 Hint

Mockito uses verify() with times() to check call counts.

🔧 Debug
advanced
2:00remaining
Debugging a Test Failure with a Fake

A test using a fake database implementation fails because data is not saved as expected. Which is the most likely cause?

AThe mock was not verified
BThe fake does not implement the save method correctly
CThe stub returns incorrect data
DThe spy did not record interactions
Attempts:
2 left
💡 Hint

Fakes have working implementations; if data is not saved, the implementation may be wrong.

framework
expert
3:00remaining
Choosing the Right Test Double in Complex JUnit Tests

You have a service that depends on a slow external API. You want to test your service's logic without calling the real API, but also want to verify that your service calls the API with correct parameters. Which test double combination is best?

AUse a stub to simulate API responses and a spy to verify method calls
BUse a fake with a working API implementation only
CUse a mock to simulate API and verify calls, no stub needed
DUse a spy only to simulate and verify API calls
Attempts:
2 left
💡 Hint

Consider separating behavior simulation and interaction verification.