Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to mock a dependency using @MockBean in a Spring Boot test.
Spring Boot
@SpringBootTest public class UserServiceTest { @[1] private UserRepository userRepository; @Autowired private UserService userService; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @MockBean for mocking.
Confusing @InjectMocks with Spring annotations.
✗ Incorrect
The @MockBean annotation is used to create and inject a mock of the UserRepository into the Spring test context.
2fill in blank
mediumComplete the code to define behavior for the mocked repository method.
Spring Boot
when(userRepository.[1](anyLong())).thenReturn(Optional.of(new User())); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using save or deleteById which do not return Optional.
Using findAll which returns a list, not Optional.
✗ Incorrect
The findById method is used to fetch a user by ID, which is commonly mocked in tests.
3fill in blank
hardFix the error in the test annotation to enable Mockito support.
Spring Boot
@SpringBootTest @[1] public class ProductServiceTest { @MockBean private ProductRepository productRepository; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using MockitoExtension which is for pure Mockito tests, not Spring Boot tests.
Using non-existent annotations like EnableMocking.
✗ Incorrect
The @RunWith(SpringRunner.class) annotation integrates Spring TestContext Framework with JUnit and enables Mockito support.
4fill in blank
hardFill both blanks to mock a service bean and inject it into the test class.
Spring Boot
@SpringBootTest public class OrderServiceTest { @[1] private PaymentService paymentService; @Autowired private OrderService orderService; @Test public void testOrder() { when(paymentService.[2](anyDouble())).thenReturn(true); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @MockBean for mocking.
Mocking the wrong method name.
✗ Incorrect
Use @MockBean to mock PaymentService and define behavior for processPayment method.
5fill in blank
hardFill all three blanks to mock a repository, define a method behavior, and verify interaction.
Spring Boot
@SpringBootTest public class CustomerServiceTest { @[1] private CustomerRepository customerRepository; @Autowired private CustomerService customerService; @Test public void testFindCustomer() { when(customerRepository.[2](anyString())).thenReturn(Optional.of(new Customer())); customerService.findCustomerByEmail("test@example.com"); verify(customerRepository).[3]("test@example.com"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using save instead of findByEmail for mocking and verification.
Not using @MockBean for mocking the repository.
✗ Incorrect
Use @MockBean to mock the repository, mock findByEmail method, and verify it was called with the email.