0
0
JUnittesting~20 mins

Mocking void methods in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Void Method Mocking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of mocking a void method with doNothing()
What will be the output of the following JUnit test using Mockito when the void method is mocked with doNothing()?
JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;

class Service {
    void performAction() {
        System.out.println("Action performed");
    }
}

public class ServiceTest {
    @Test
    void testPerformAction() {
        Service mockService = mock(Service.class);
        doNothing().when(mockService).performAction();
        mockService.performAction();
        System.out.println("Test completed");
    }
}
AAction performed\nTest completed
BNo output
CNullPointerException
DTest completed
Attempts:
2 left
💡 Hint
Think about what doNothing() does when mocking void methods.
assertion
intermediate
2:00remaining
Correct assertion to verify a void method call
Which assertion correctly verifies that a void method performAction() was called exactly once on a mock object in JUnit with Mockito?
JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;

class Service {
    void performAction() {}
}

public class ServiceTest {
    @Test
    void testVerifyCall() {
        Service mockService = mock(Service.class);
        mockService.performAction();
        // Which assertion is correct here?
    }
}
Averify(mockService).performAction(1);
BassertTrue(mockService.performAction());
Cverify(mockService, times(1)).performAction();
DassertEquals(1, mockService.performAction());
Attempts:
2 left
💡 Hint
Mockito uses verify() to check method calls on mocks.
🔧 Debug
advanced
2:00remaining
Debugging a test with doThrow on a void method
Given the following test, what error will be thrown when mockService.performAction() is called?
JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;

class Service {
    void performAction() {}
}

public class ServiceTest {
    @Test
    void testException() {
        Service mockService = mock(Service.class);
        doThrow(new RuntimeException("Error occurred")).when(mockService).performAction();
        mockService.performAction();
    }
}
ANullPointerException
BRuntimeException with message "Error occurred"
CMockitoException
DNo exception, method does nothing
Attempts:
2 left
💡 Hint
doThrow() makes the mocked void method throw an exception when called.
🧠 Conceptual
advanced
2:00remaining
Why use doAnswer() with void methods in Mockito?
Why would a tester use doAnswer() when mocking a void method instead of doNothing() or doThrow()?
ATo execute custom code or logic when the void method is called
BTo automatically call the real method implementation
CTo ignore the method call silently without any effect
DTo verify the method was never called
Attempts:
2 left
💡 Hint
doAnswer() lets you define what happens when the method is called.
framework
expert
3:00remaining
Correct Mockito syntax to mock a void method throwing checked exception
Which option correctly mocks a void method performAction() to throw a checked exception IOException when called, using Mockito in JUnit?
JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import java.io.IOException;

class Service {
    void performAction() throws IOException {}
}

public class ServiceTest {
    @Test
    void testCheckedException() throws IOException {
        Service mockService = mock(Service.class);
        // Which option correctly mocks performAction() to throw IOException?
    }
}
AdoThrow(new IOException("IO error")).when(mockService).performAction();
Bwhen(mockService.performAction()).thenThrow(new IOException("IO error"));
CdoThrow(IOException.class).when(mockService).performAction();
Dwhen(mockService.performAction()).thenReturn(new IOException("IO error"));
Attempts:
2 left
💡 Hint
Use doThrow() for void methods throwing checked exceptions.