0
0
JUnittesting~20 mins

Dummy objects in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dummy Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
    }
}
ATest passes because dummy object satisfies the dependency.
BTest fails because DummyRepository does not implement any methods.
CTest throws NullPointerException at runtime.
DTest fails due to compilation error: missing method implementation.
Attempts:
2 left
💡 Hint
Dummy objects provide minimal implementation just to satisfy dependencies.
assertion
intermediate
2: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)?
AassertEquals(dummy, processor.process(dummy));
BassertDoesNotThrow(() -> processor.process(dummy));
CassertNull(processor.process(dummy));
DassertThrows(Exception.class, () -> processor.process(dummy));
Attempts:
2 left
💡 Hint
The method returns void and does nothing, so check it does not throw exceptions.
🔧 Debug
advanced
2: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);
    }
}
ACompilation error because DummyLogger is missing interface implementation.
BService constructor throws NullPointerException due to dummy being null.
CDummyLogger does not implement log method, causing AbstractMethodError at runtime.
DTest fails assertion because assertTrue(true) is false.
Attempts:
2 left
💡 Hint
Check if DummyLogger properly implements all interface methods.
🧠 Conceptual
advanced
2:00remaining
Purpose of dummy objects in unit testing
Which statement best describes the purpose of dummy objects in unit testing?
AThey generate random data inputs to test edge cases automatically.
BThey simulate complex behavior to test interactions between components.
CThey replace real objects to verify method calls and returned values.
DThey provide minimal implementations to satisfy method parameters without affecting test behavior.
Attempts:
2 left
💡 Hint
Think about why you would pass an object that is not used in the test.
framework
expert
2: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 ServiceClient
AServiceClient dummy = Mockito.spy(ServiceClient.class);
BServiceClient dummy = new ServiceClient() {};
CServiceClient dummy = Mockito.mock(ServiceClient.class);
DServiceClient dummy = Mockito.create(ServiceClient.class);
Attempts:
2 left
💡 Hint
Mockito.mock creates a dummy object for interfaces.