0
0
Spring Bootframework~10 mins

@MockBean for mocking dependencies in Spring Boot - Interactive Code Practice

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

Complete 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'
AService
BAutowired
CInjectMocks
DMockBean
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @MockBean for mocking.
Confusing @InjectMocks with Spring annotations.
2fill in blank
medium

Complete 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'
Asave
BfindAll
CfindById
DdeleteById
Attempts:
3 left
💡 Hint
Common Mistakes
Using save or deleteById which do not return Optional.
Using findAll which returns a list, not Optional.
3fill in blank
hard

Fix 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'
ARunWith(SpringRunner.class)
BExtendWith(MockitoExtension.class)
CEnableMocking
DMockitoTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using MockitoExtension which is for pure Mockito tests, not Spring Boot tests.
Using non-existent annotations like EnableMocking.
4fill in blank
hard

Fill 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'
AMockBean
BAutowired
CprocessPayment
DcancelPayment
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @MockBean for mocking.
Mocking the wrong method name.
5fill in blank
hard

Fill 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'
AMockBean
BfindByEmail
Dsave
Attempts:
3 left
💡 Hint
Common Mistakes
Using save instead of findByEmail for mocking and verification.
Not using @MockBean for mocking the repository.