0
0
JUnittesting~20 mins

Why mocking isolates units under test in JUnit - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mocking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use mocking in unit tests?

In unit testing with JUnit, why do we use mocking frameworks like Mockito?

ATo replace real dependencies with controlled fake objects, isolating the unit under test.
BTo speed up the test execution by running tests in parallel automatically.
CTo generate test data automatically without writing any code.
DTo convert integration tests into system tests for better coverage.
Attempts:
2 left
💡 Hint

Think about how dependencies affect the behavior of the unit you want to test.

Predict Output
intermediate
2:00remaining
Output of a JUnit test using a mock

What is the output of this JUnit test when the mocked dependency returns 5?

JUnit
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class Calculator {
    Dependency dep;
    Calculator(Dependency dep) { this.dep = dep; }
    int compute() { return dep.getValue() * 2; }
}

interface Dependency {
    int getValue();
}

public class CalculatorTest {
    @Test
    void testCompute() {
        Dependency mockDep = mock(Dependency.class);
        when(mockDep.getValue()).thenReturn(5);
        Calculator calc = new Calculator(mockDep);
        assertEquals(10, calc.compute());
    }
}
ATest passes because compute() returns 10 as expected.
BTest fails because compute() returns 5 instead of 10.
CTest fails with NullPointerException because mock is not initialized.
DTest fails due to a compilation error in the mock setup.
Attempts:
2 left
💡 Hint

Check what value the mock returns and how compute() uses it.

assertion
advanced
2:00remaining
Correct assertion to verify mock interaction

Which assertion correctly verifies that the mocked dependency's method was called exactly once?

JUnit
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;

interface Service {
    void perform();
}

public class ServiceTest {
    @Test
    void testPerformCalled() {
        Service mockService = mock(Service.class);
        mockService.perform();
        // Which assertion below is correct?
    }
}
AassertEquals(1, mockService.perform());
Bverify(mockService, times(1)).perform();
Cverify(mockService).perform(1);
DassertTrue(mockService.perform() == 1);
Attempts:
2 left
💡 Hint

Mockito uses verify() to check method calls, not assertEquals.

🔧 Debug
advanced
2:00remaining
Why does this mock cause NullPointerException?

Given this test code, why does it throw NullPointerException?

JUnit
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class Processor {
    Helper helper;
    Processor(Helper helper) { this.helper = helper; }
    int process() { return helper.getData() + 1; }
}

interface Helper {
    Integer getData();
}

public class ProcessorTest {
    @Test
    void testProcess() {
        Helper mockHelper = mock(Helper.class);
        Processor proc = new Processor(mockHelper);
        assertEquals(2, proc.process());
    }
}
AThe test assertion is incorrect, causing the exception.
BThe Processor constructor is missing, causing the exception.
CThe mockHelper.getData() returns null by default, causing NullPointerException when adding 1.
DThe mockHelper is not initialized properly, so it is null causing the exception.
Attempts:
2 left
💡 Hint

What does a mock return if no behavior is defined?

framework
expert
3:00remaining
How mocking isolates units in complex integration scenarios

In a complex system with multiple dependencies, how does mocking help isolate the unit under test in JUnit?

ABy automatically generating integration test cases from unit tests.
BBy running all dependent modules in parallel to speed up integration testing.
CBy disabling all logging and monitoring during tests to reduce noise.
DBy replacing all external dependencies with mocks, tests focus only on the unit's logic without side effects or delays.
Attempts:
2 left
💡 Hint

Think about controlling dependencies to avoid unpredictable behavior.