0
0
JUnittesting~10 mins

@MockBean for Spring mocking 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 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'
AAutowired
BInjectMocks
CMockBean
DMock
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mock instead of @MockBean in Spring Boot tests.
Forgetting to annotate the test class with @SpringBootTest.
2fill in blank
medium

Complete 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'
AuserRepository
BmockUserRepository
Crepository
DuserService
Attempts:
3 left
💡 Hint
Common Mistakes
Verifying the service instead of the repository mock.
Using a variable name not declared in the test.
3fill in blank
hard

Fix 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'
AMock
BMockBean
CInjectMocks
DAutowired
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mock instead of @MockBean causing Spring context errors.
Not annotating the test class with @SpringBootTest.
4fill in blank
hard

Fill 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'
AMockBean
BInjectMocks
CopenMocks
DinitMocks
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Mock instead of @MockBean in Spring tests.
Using deprecated initMocks instead of openMocks.
5fill in blank
hard

Fill 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'
AMockBean
BinventoryService
C5
DMock
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.