Bird
Raised Fist0
JUnittesting~20 mins

Mockito dependency setup in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Mockito Dependency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Mockito Dependency in Maven
Which Maven dependency snippet correctly adds Mockito version 4.11.0 to a JUnit 5 project?
A
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>4.11.0</version>
  <scope>test</scope>
</dependency>
B
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>4.11.0</version>
</dependency>
C
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-junit-jupiter</artifactId>
  <version>4.11.0</version>
  <scope>test</scope>
</dependency>
D
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>3.5.0</version>
  <scope>compile</scope>
</dependency>
Attempts:
2 left
💡 Hint
Mockito core is the main library; use the latest version and test scope for testing.
❓ Predict Output
intermediate
2:00remaining
JUnit 5 Test Execution with Mockito Dependency
Given the following JUnit 5 test class using Mockito, what will be the test execution result if Mockito dependency is missing from the project?
JUnit
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class SampleTest {
    @Test
    void testMock() {
        Runnable mockRunnable = Mockito.mock(Runnable.class);
        Mockito.verify(mockRunnable, Mockito.never()).run();
    }
}
ATest passes successfully.
BTest fails with a NullPointerException.
CTest fails with an AssertionError.
DTest fails with a ClassNotFoundException for Mockito classes.
Attempts:
2 left
💡 Hint
Think about what happens if the Mockito classes are not found at runtime.
❓ locator
advanced
2:00remaining
Correct Mockito Dependency for Gradle Build
Which Gradle dependency line correctly adds Mockito version 4.11.0 for testing in a Java project?
Aimplementation 'org.mockito:mockito-core:4.11.0'
BtestImplementation 'org.mockito:mockito-core:4.11.0'
CcompileOnly 'org.mockito:mockito-core:4.11.0'
DruntimeOnly 'org.mockito:mockito-core:4.11.0'
Attempts:
2 left
💡 Hint
Mockito is used only during testing, so use the testImplementation configuration.
❓ assertion
advanced
2:00remaining
Mockito Version Compatibility Assertion
Which assertion correctly verifies that the Mockito version used in the project is at least 4.0.0 in a Java test?
JUnit
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.mockito.Mockito;

public class VersionTest {
    @org.junit.jupiter.api.Test
    void testMockitoVersion() {
        String version = Mockito.class.getPackage().getImplementationVersion();
        // Insert assertion here
    }
}
AassertTrue(version.equals("4.0.0"));
BassertTrue(version.startsWith("4"));
CassertTrue(version.compareTo("4.0.0") >= 0);
DassertTrue(version.compareTo("3.9.9") > 0);
Attempts:
2 left
💡 Hint
Use string comparison to check version is equal or higher than 4.0.0.
❓ framework
expert
3:00remaining
Mockito Extension Setup for JUnit 5
Which code snippet correctly enables Mockito annotations like @Mock in a JUnit 5 test class?
A
@ExtendWith(MockitoExtension.class)
public class MyTest {
  @Mock
  private List<String> mockList;

  @Test
  void test() {
    Mockito.when(mockList.size()).thenReturn(5);
    assertEquals(5, mockList.size());
  }
}
B
@RunWith(MockitoJUnitRunner.class)
public class MyTest {
  @Mock
  private List<String> mockList;

  @Test
  void test() {
    Mockito.when(mockList.size()).thenReturn(5);
    assertEquals(5, mockList.size());
  }
}
C
public class MyTest {
  @Mock
  private List<String> mockList;

  @BeforeEach
  void init() {
    MockitoAnnotations.openMocks(this);
  }

  @Test
  void test() {
    Mockito.when(mockList.size()).thenReturn(5);
    assertEquals(5, mockList.size());
  }
}
D
public class MyTest {
  private List<String> mockList = Mockito.mock(List.class);

  @Test
  void test() {
    Mockito.when(mockList.size()).thenReturn(5);
    assertEquals(5, mockList.size());
  }
}
Attempts:
2 left
💡 Hint
JUnit 5 uses @ExtendWith for extensions, not @RunWith.

Practice

(1/5)
1. What is the main purpose of setting up Mockito as a dependency in a JUnit project?
easy
A. To automatically generate user interfaces
B. To create fake objects for testing without using real implementations
C. To improve the performance of the application in production
D. To deploy the application to a server

Solution

  1. Step 1: Understand Mockito's role

    Mockito is a tool used to create mock objects that simulate real objects in tests.
  2. Step 2: Identify the purpose in testing

    Using mocks helps isolate the code under test by replacing dependencies with fake versions.
  3. Final Answer:

    To create fake objects for testing without using real implementations -> Option B
  4. Quick Check:

    Mockito = mock objects for testing [OK]
Hint: Mockito creates fake objects to test code easily [OK]
Common Mistakes:
  • Thinking Mockito improves production performance
  • Confusing Mockito with UI tools
  • Assuming Mockito deploys applications
2. Which of the following is the correct Maven dependency snippet to add Mockito version 4.11.0 for test scope?
easy
A. org.mockito mockito-core 4.11.0 test
B. org.mockito mockito-core 4.11.0
C. org.mockito mockito-all 3.0.0 compile
D. org.mockito mockito-core 5.0.0 test

Solution

  1. Step 1: Check groupId and artifactId

    The correct groupId is 'org.mockito' and artifactId is 'mockito-core' for Mockito library.
  2. Step 2: Verify version and scope

    Version 4.11.0 is correct and scope should be 'test' to use Mockito only during testing.
  3. Final Answer:

    Correct Maven dependency with version 4.11.0 and test scope -> Option A
  4. Quick Check:

    Maven dependency with test scope = org.mockito mockito-core 4.11.0 test [OK]
Hint: Use 'mockito-core' with test scope and correct version [OK]
Common Mistakes:
  • Omitting the test scope
  • Using wrong artifactId like 'mockito-all'
  • Using incorrect or too new version
3. You want to use Mockito in a JUnit 5 project with Gradle. Which of the following Gradle dependency setups correctly adds Mockito version 4.11.0 only for test implementation?
easy
A. testImplementation 'org.mockito:mockito-core:4.11.0'
B. implementation 'org.mockito:mockito-core:4.11.0'
C. compileOnly 'org.mockito:mockito-core:4.11.0'
D. runtimeOnly 'org.mockito:mockito-core:4.11.0'

Solution

  1. Step 1: Understand Gradle scopes

    testImplementation means the dependency is available only during test compilation and runtime.
  2. Step 2: Choose correct scope for Mockito

    Mockito is used only in tests, so testImplementation is the correct scope.
  3. Final Answer:

    testImplementation 'org.mockito:mockito-core:4.11.0' -> Option A
  4. Quick Check:

    Mockito test dependency = testImplementation [OK]
Hint: Use testImplementation for test-only dependencies in Gradle [OK]
Common Mistakes:
  • Using implementation instead of testImplementation
  • Using compileOnly which excludes runtime
  • Using runtimeOnly which excludes compile time
4. Given the following JUnit test code snippet using Mockito, what will be the output of System.out.println(mockService.getData());?
import static org.mockito.Mockito.*;

public class ServiceTest {
  @Test
  void testGetData() {
    DataService mockService = mock(DataService.class);
    when(mockService.getData()).thenReturn("Mocked Data");
    System.out.println(mockService.getData());
  }
}
medium
A. Original data from DataService
B. null
C. Throws NullPointerException
D. Mocked Data

Solution

  1. Step 1: Understand mock behavior

    The mock object returns what is specified in when().thenReturn(), here "Mocked Data".
  2. Step 2: Check the printed output

    Calling getData() on mockService returns "Mocked Data", so it prints that string.
  3. Final Answer:

    Mocked Data -> Option D
  4. Quick Check:

    Mockito when().thenReturn() sets output = Mocked Data [OK]
Hint: when().thenReturn() sets mock method output [OK]
Common Mistakes:
  • Expecting null because it's a mock
  • Assuming real method runs
  • Thinking it throws an exception
5. Identify the error in the following Mockito setup code snippet:
import static org.mockito.Mockito.*;

public class UserServiceTest {
  @Mock
  UserRepository repo;

  @Test
  void testUser() {
    when(repo.findUser()).thenReturn(new User("Alice"));
    UserService service = new UserService(repo);
    assertEquals("Alice", service.getUserName());
  }
}
medium
A. Incorrect method name findUser() should be getUser()
B. UserService constructor should not accept repo
C. Missing MockitoAnnotations.openMocks(this) to initialize @Mock fields
D. assertEquals parameters are reversed

Solution

  1. Step 1: Check @Mock initialization

    @Mock fields need initialization, usually by calling MockitoAnnotations.openMocks(this) in setup.
  2. Step 2: Verify test setup completeness

    Without initialization, repo remains null causing NullPointerException when used.
  3. Final Answer:

    Missing MockitoAnnotations.openMocks(this) to initialize @Mock fields -> Option C
  4. Quick Check:

    @Mock requires initialization = Missing MockitoAnnotations.openMocks(this) to initialize @Mock fields [OK]
Hint: Always initialize @Mock with MockitoAnnotations.openMocks(this) [OK]
Common Mistakes:
  • Forgetting to initialize @Mock fields
  • Assuming @Mock works without setup
  • Mixing method names without checking
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