What if you could test your code without waiting for slow or unreliable external services?
Why Fake objects in JUnit? - Purpose & Use Cases
Imagine testing a payment system that talks to a real bank every time you run a test.
You have to wait for the bank's response, and sometimes the bank service is down or slow.
This makes testing frustrating and unreliable.
Manually testing with real services is slow and unpredictable.
Tests can fail because of network issues or external service downtime, not because your code is wrong.
This wastes time and causes confusion.
Fake objects act like the real services but run instantly and reliably inside your tests.
They let you control responses and test your code without waiting or depending on outside systems.
PaymentService payment = new RealBankPaymentService();
payment.process(amount); // waits for real bank responsePaymentService payment = new FakeBankPaymentService(); payment.process(amount); // instant, controlled response
Fake objects let you test your code quickly and reliably without depending on real external systems.
When testing an online store, you use a fake payment service to simulate credit card approval or decline instantly, so you can test checkout flows without charging real cards.
Manual testing with real services is slow and unreliable.
Fake objects simulate real services inside tests for speed and control.
This makes tests fast, stable, and easier to write.