0
0
JUnittesting~10 mins

@InjectMocks annotation in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a mock object for the dependency.

JUnit
@Mock
private Service [1];
Drag options to blanks, or click blank then click option'
Aservice
BService
CmockService
DserviceMock
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name instead of a variable name.
Adding prefixes like 'mock' unnecessarily.
2fill in blank
medium

Complete the code to inject mocks into the tested class.

JUnit
@InjectMocks
private [1] controller;
Drag options to blanks, or click blank then click option'
Aservice
Bcontroller
CService
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name instead of the class type.
Confusing the service class with the controller class.
3fill in blank
hard

Fix the error in the test setup method to initialize mocks.

JUnit
@BeforeEach
void setUp() {
    MockitoAnnotations.[1](this);
}
Drag options to blanks, or click blank then click option'
AopenMocks
BinitMocks
CmockStatic
Dmock
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated initMocks method.
Calling mockStatic instead of initializing instance mocks.
4fill in blank
hard

Fill both blanks to correctly declare and inject mocks for dependencies.

JUnit
@Mock
private [1] repository;

@InjectMocks
private [2] service;
Drag options to blanks, or click blank then click option'
ARepository
BService
Crepository
Dservice
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names instead of class types for mocks.
Mixing up the order of mock and injectMocks declarations.
5fill in blank
hard

Fill all three blanks to complete the test class setup with mocks and injection.

JUnit
class UserServiceTest {

    @Mock
    private [1] userRepository;

    @InjectMocks
    private [2] userService;

    @BeforeEach
    void init() {
        MockitoAnnotations.[3](this);
    }
}
Drag options to blanks, or click blank then click option'
AUserRepository
BUserService
CopenMocks
DinitMocks
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated initMocks instead of openMocks.
Mixing variable names with class types in annotations.