Bird
Raised Fist0
JUnittesting~20 mins

Dummy objects in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a dummy object in JUnit testing?
easy
A. To perform actual logic and return real data
B. To replace the entire system under test
C. To fill method parameters when the object is not used in the test
D. To generate random test data automatically

Solution

  1. Step 1: Understand dummy object role

    Dummy objects are simple placeholders used only to fill method parameters without affecting test logic.
  2. Step 2: Differentiate from other test doubles

    Unlike mocks or stubs, dummy objects do not perform any behavior or return values.
  3. Final Answer:

    To fill method parameters when the object is not used in the test -> Option C
  4. Quick Check:

    Dummy object = placeholder parameter [OK]
Hint: Dummy objects fill unused parameters only [OK]
Common Mistakes:
  • Confusing dummy objects with mocks or stubs
  • Thinking dummy objects execute logic
  • Using dummy objects to test behavior
2. Which of the following is the correct way to create a dummy object for a parameter of type List<String> in JUnit?
easy
A. List<String> dummy = new LinkedList<>(Arrays.asList("test"));
B. List<String> dummy = new ArrayList<>();
C. List<String> dummy = null;
D. List<String> dummy = Mockito.mock(List.class);

Solution

  1. Step 1: Identify dummy object creation

    A dummy object should be a simple instance without behavior, so creating an empty ArrayList works.
  2. Step 2: Evaluate other options

    Null is not a dummy object; a LinkedList with data is more than dummy; Mockito.mock creates a mock, not a dummy.
  3. Final Answer:

    List<String> dummy = new ArrayList<>(); -> Option B
  4. Quick Check:

    Empty simple instance = dummy object [OK]
Hint: Use simple empty instance for dummy objects [OK]
Common Mistakes:
  • Using null instead of a dummy object
  • Creating mocks instead of dummies
  • Adding data to dummy objects unnecessarily
3. Consider the following JUnit test snippet:
public void testProcess() {
    Processor processor = new Processor();
    Data dummyData = new Data(); // dummy object
    boolean result = processor.process(dummyData);
    assertTrue(result);
}

What is the role of dummyData here?
medium
A. It acts as a dummy object to satisfy method parameter
B. It provides real data for processing
C. It mocks the Data class behavior
D. It stubs the process method output

Solution

  1. Step 1: Analyze dummyData usage

    dummyData is created but not configured or used to affect logic, so it acts as a placeholder.
  2. Step 2: Understand dummy vs mock/stub

    It is not mocking or stubbing behavior, just filling the parameter for process method.
  3. Final Answer:

    It acts as a dummy object to satisfy method parameter -> Option A
  4. Quick Check:

    Dummy object = unused parameter filler [OK]
Hint: Dummy objects fill parameters without behavior [OK]
Common Mistakes:
  • Assuming dummyData affects test logic
  • Confusing dummy with mock or stub
  • Thinking dummyData controls output
4. Identify the error in this JUnit test using a dummy object:
@Test
public void testCalculate() {
    Calculator calc = new Calculator();
    int result = calc.calculate(null); // passing null as dummy
    assertEquals(0, result);
}
medium
A. Test method missing @Before annotation
B. The assertEquals syntax is incorrect
C. Calculator class cannot be instantiated
D. Passing null instead of a dummy object may cause NullPointerException

Solution

  1. Step 1: Check dummy object usage

    Passing null is not a dummy object; it may cause NullPointerException if method uses the parameter.
  2. Step 2: Verify other options

    assertEquals syntax is correct; Calculator instantiation is valid; @Before is optional here.
  3. Final Answer:

    Passing null instead of a dummy object may cause NullPointerException -> Option D
  4. Quick Check:

    Null ≠ dummy object [OK]
Hint: Never use null as dummy object [OK]
Common Mistakes:
  • Using null instead of dummy object
  • Ignoring possible NullPointerException
  • Confusing test annotations
5. You have a method sendEmail(User user, EmailContent content) where content is not used in your test. How should you use a dummy object for content in your JUnit test to keep it clean and safe?
hard
A. Create an empty EmailContent instance with default constructor
B. Pass null for content parameter
C. Use a Mockito mock for EmailContent
D. Create EmailContent with real data for testing

Solution

  1. Step 1: Understand dummy object purpose

    Since content is unused, a simple empty instance suffices as dummy.
  2. Step 2: Avoid null and mocks for dummy

    Null risks errors; mocks add complexity; real data is unnecessary here.
  3. Final Answer:

    Create an empty EmailContent instance with default constructor -> Option A
  4. Quick Check:

    Empty instance = dummy object [OK]
Hint: Use simple empty instance for unused parameters [OK]
Common Mistakes:
  • Passing null causing NullPointerException
  • Using mocks instead of dummy objects
  • Adding unnecessary real data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard