Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a mock bean for the UserService in a Spring test.
JUnit
@SpringBootTest public class UserServiceTest { @[1] private UserService userService; @Test public void testUser() { // test logic here } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mock instead of @MockBean in Spring Boot tests.
Forgetting to annotate the test class with @SpringBootTest.
✗ Incorrect
The @MockBean annotation creates a mock of the UserService and adds it to the Spring context for testing.
2fill in blank
mediumComplete the code to verify that the mocked UserRepository is called once in the test.
JUnit
@MockBean
private UserRepository userRepository;
@Test
public void testFindUser() {
userService.findUserById(1L);
verify([1]).findById(1L);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Verifying the service instead of the repository mock.
Using a variable name not declared in the test.
✗ Incorrect
We verify the mock userRepository to check if its findById method was called with argument 1L.
3fill in blank
hardFix the error in the test setup by completing the annotation to mock the EmailService bean.
JUnit
@SpringBootTest public class NotificationTest { @[1] private EmailService emailService; @Test public void sendEmailTest() { when(emailService.sendEmail(anyString())).thenReturn(true); // test logic } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mock instead of @MockBean causing Spring context errors.
Not annotating the test class with @SpringBootTest.
✗ Incorrect
Use @MockBean to mock EmailService in Spring Boot tests so Spring context uses the mock.
4fill in blank
hardFill both blanks to mock the PaymentService and inject the mock into OrderService.
JUnit
@SpringBootTest public class OrderServiceTest { @[1] private PaymentService paymentService; @Autowired private OrderService orderService; @BeforeEach public void setup() { MockitoAnnotations.[2](this); } @Test public void testPayment() { when(paymentService.processPayment(anyDouble())).thenReturn(true); // test logic } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mock instead of @MockBean in Spring tests.
Using deprecated initMocks instead of openMocks.
✗ Incorrect
Use @MockBean to mock PaymentService in Spring context. Use MockitoAnnotations.openMocks(this) to initialize mocks.
5fill in blank
hardFill all three blanks to create a mock bean for InventoryService, inject it, and verify a method call.
JUnit
@SpringBootTest public class InventoryTest { @[1] private InventoryService inventoryService; @Autowired private InventoryController inventoryController; @Test public void testInventoryUpdate() { inventoryController.updateStock(5); verify([2]).updateStock([3]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mock instead of @MockBean causing Spring context issues.
Verifying the controller instead of the mock service.
Using wrong argument in verify call.
✗ Incorrect
Use @MockBean to mock InventoryService. Verify the mock inventoryService received updateStock call with argument 5.