Challenge - 5 Problems
Dummy Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a test using a dummy object in JUnit
Consider the following JUnit test code using a dummy object. What will be the test execution result?
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class Service { private final Repository repo; Service(Repository repo) { this.repo = repo; } boolean isAvailable() { return repo != null; } } interface Repository {} class DummyRepository implements Repository {} public class DummyTest { @Test void testServiceWithDummy() { Repository dummy = new DummyRepository(); Service service = new Service(dummy); assertTrue(service.isAvailable()); } }
Attempts:
2 left
💡 Hint
Dummy objects provide minimal implementation just to satisfy dependencies.
✗ Incorrect
The dummy object DummyRepository implements the Repository interface but does not provide any methods. Since Service only checks if repo is not null, the test passes.
❓ assertion
intermediate2:00remaining
Correct assertion for verifying dummy object usage
You have a dummy object passed to a method that does not use it. Which assertion correctly verifies the method completes without errors?
JUnit
class Processor { void process(Object dummy) { /* does nothing with dummy */ } } Processor processor = new Processor(); Object dummy = new Object(); // Which assertion below is correct to test process(dummy)?
Attempts:
2 left
💡 Hint
The method returns void and does nothing, so check it does not throw exceptions.
✗ Incorrect
Since process returns void and does nothing, the correct assertion is to verify it does not throw any exceptions.
🔧 Debug
advanced2:00remaining
Identify the problem with dummy object usage in this test
This JUnit test uses a dummy object but does not compile. What is the cause?
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; interface Logger { void log(String message); } class Service { private final Logger logger; Service(Logger logger) { this.logger = logger; } void run() { logger.log("Running"); } } class DummyLogger implements Logger {} public class DummyBugTest { @Test void testRun() { Logger dummy = new DummyLogger(); Service service = new Service(dummy); service.run(); assertTrue(true); } }
Attempts:
2 left
💡 Hint
Check if DummyLogger properly implements all interface methods.
✗ Incorrect
DummyLogger does not provide an implementation for log, causing a compilation error as it must implement all abstract methods from the interface.
🧠 Conceptual
advanced2:00remaining
Purpose of dummy objects in unit testing
Which statement best describes the purpose of dummy objects in unit testing?
Attempts:
2 left
💡 Hint
Think about why you would pass an object that is not used in the test.
✗ Incorrect
Dummy objects are simple placeholders passed to satisfy method signatures but do not affect the test outcome.
❓ framework
expert2:00remaining
Using Mockito to create a dummy object in JUnit
Which Mockito code snippet correctly creates a dummy object for interface ServiceClient to pass into a test?
JUnit
interface ServiceClient {
void send(String data);
}
// Choose the correct Mockito code to create a dummy ServiceClientAttempts:
2 left
💡 Hint
Mockito.mock creates a dummy object for interfaces.
✗ Incorrect
Mockito.mock(ServiceClient.class) creates a dummy object implementing ServiceClient with no behavior.