0
0
Spring Bootframework~20 mins

@MockBean for mocking dependencies in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mocking Mastery with @MockBean
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when @MockBean is used on a service in a Spring Boot test?
Consider a Spring Boot test class where a service is annotated with @MockBean. What is the effect of this annotation on the application context during the test?
Spring Boot
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class UserServiceTest {

    @MockBean
    private UserRepository userRepository;

    // test methods
}
AThe UserRepository bean is disabled and no bean is available in the test context.
BThe real UserRepository bean is replaced by a Mockito mock in the test context.
CA new instance of UserRepository is created without any mocking.
DThe UserRepository bean is proxied but calls go to the real implementation.
Attempts:
2 left
💡 Hint
Think about how Spring Boot replaces beans during testing to isolate dependencies.
📝 Syntax
intermediate
2:00remaining
Identify the correct way to declare a @MockBean in a Spring Boot test
Which of the following code snippets correctly declares a @MockBean for a service in a Spring Boot test class?
A
@MockBean
private UserService userService;
B
@MockBean
UserService userService = new UserService();
C
@MockBean
public UserService userService() { return new UserService(); }
D
@MockBean(UserService.class)
private UserService userService;
Attempts:
2 left
💡 Hint
Remember how fields are annotated for mocking in Spring Boot tests.
🔧 Debug
advanced
2:00remaining
Why does the test fail to inject the mocked bean with @MockBean?
Given the following test code, the test fails because the mocked bean is not injected properly. What is the most likely cause?
 @SpringBootTest
public class OrderServiceTest {

    @MockBean
    private PaymentService paymentService;

    @Autowired
    private OrderService orderService;

    @Test
    public void testOrder() {
        // test logic
    }
}
AThe PaymentService bean is final and cannot be mocked by Mockito.
BThe @MockBean annotation is missing the required parameter to specify the bean class.
CThe test class is missing @ExtendWith(MockitoExtension.class) annotation.
DThe OrderService bean is not a Spring-managed bean, so injection fails.
Attempts:
2 left
💡 Hint
Check if the service under test is managed by Spring context.
state_output
advanced
2:00remaining
What is the output of the test when using @MockBean with a stubbed method?
Given the following test snippet, what will be the output printed?
 @SpringBootTest
public class GreetingServiceTest {

    @MockBean
    private TimeService timeService;

    @Autowired
    private GreetingService greetingService;

    @Test
    public void testGreeting() {
        when(timeService.getCurrentHour()).thenReturn(10);
        System.out.println(greetingService.greet());
    }
}

// GreetingService calls timeService.getCurrentHour() and returns "Good morning" if hour < 12, else "Good afternoon".
Anull
BGood afternoon
CGood morning
DThrows NullPointerException
Attempts:
2 left
💡 Hint
Consider the stubbed return value of the mocked method.
🧠 Conceptual
expert
3:00remaining
Why use @MockBean instead of Mockito.mock() in Spring Boot tests?
Which of the following best explains the advantage of using @MockBean over directly creating mocks with Mockito.mock() in Spring Boot integration tests?
A@MockBean replaces the bean in the Spring application context, ensuring all dependencies receive the mock automatically.
B@MockBean creates a mock that can only be used in unit tests, not integration tests.
CMockito.mock() automatically registers the mock in the Spring context, so @MockBean is redundant.
D@MockBean disables all other beans except the mocked one, speeding up tests.
Attempts:
2 left
💡 Hint
Think about how Spring manages beans and dependencies during tests.