0
0
JUnittesting~20 mins

@InjectMocks annotation in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mockito Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of @InjectMocks in JUnit Testing

What is the main purpose of the @InjectMocks annotation in JUnit tests using Mockito?

AIt creates an instance of the class and injects mocked dependencies into it automatically.
BIt resets all mocks before each test method runs.
CIt verifies that a method was called on a mock object.
DIt marks a field as a mock object to be used in the test.
Attempts:
2 left
💡 Hint

Think about how dependencies are provided to the class under test.

Predict Output
intermediate
2:00remaining
Output of Test with @InjectMocks and @Mock

Consider the following JUnit test code snippet using Mockito:

public class ServiceTest {

  @Mock
  Repository repo;

  @InjectMocks
  Service service;

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

  @Test
  void testGetData() {
    when(repo.getData()).thenReturn("mocked data");
    String result = service.getData();
    System.out.println(result);
  }
}

class Service {
  private Repository repo;
  public Service(Repository repo) { this.repo = repo; }
  public String getData() { return repo.getData(); }
}

interface Repository {
  String getData();
}

What will be printed when testGetData() runs?

AThrows IllegalStateException
Bnull
CThrows NullPointerException
Dmocked data
Attempts:
2 left
💡 Hint

Check how @InjectMocks injects the mock repo into service.

assertion
advanced
1:30remaining
Correct Assertion for @InjectMocks Behavior

Given the following test setup:

@Mock
Database db;

@InjectMocks
UserService userService;

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

@Test
void testUserCount() {
  when(db.countUsers()).thenReturn(5);
  int count = userService.getUserCount();
  // Which assertion correctly verifies the behavior?
}
AassertEquals(5, count);
BassertTrue(count > 5);
CassertNull(userService);
DassertEquals(0, count);
Attempts:
2 left
💡 Hint

Remember what value the mock db.countUsers() returns.

🔧 Debug
advanced
2:00remaining
Why is @InjectMocks Not Injecting Mocks?

In this test code, @InjectMocks does not inject the mocked dependencies, causing a NullPointerException:

@Mock
Dependency dep;

@InjectMocks
MainClass main;

@Test
void testMethod() {
  MockitoAnnotations.openMocks(this);
  main.call();
}

What is the most likely cause of this problem?

AMockitoAnnotations.openMocks(this) is called after the test method instead of before.
BThe <code>dep</code> field is not annotated with <code>@Spy</code>.
CThe <code>MainClass</code> does not have a constructor or setter for <code>dep</code>.
DThe test class is missing the <code>@RunWith(MockitoJUnitRunner.class)</code> annotation.
Attempts:
2 left
💡 Hint

Check how Mockito injects mocks into the class under test.

framework
expert
2:30remaining
Behavior of @InjectMocks with Multiple Constructors

Consider a class with multiple constructors:

public class Processor {
  private final ServiceA a;
  private final ServiceB b;

  public Processor(ServiceA a) { this.a = a; this.b = null; }
  public Processor(ServiceA a, ServiceB b) { this.a = a; this.b = b; }

  public String process() {
    return (b == null) ? a.action() : b.action();
  }
}

In a JUnit test with Mockito, @InjectMocks is used on Processor and @Mock on ServiceA and ServiceB. Which constructor will Mockito use to inject mocks?

AThe constructor with the fewest parameters (ServiceA only).
BThe constructor with the most parameters that can be satisfied by mocks (ServiceA, ServiceB).
CMockito will throw an exception due to multiple constructors.
DMockito will use field injection instead of constructor injection.
Attempts:
2 left
💡 Hint

Mockito prefers constructors with more parameters if mocks are available.