0
0
JUnittesting~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring MockBean Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Spring test with @MockBean?

Consider this Spring Boot test class using @MockBean to mock a service. What will be the printed output when the test runs?

JUnit
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import static org.mockito.BDDMockito.given;

@SpringBootTest
class UserServiceTest {

    @MockBean
    private UserRepository userRepository;

    @Autowired
    private UserService userService;

    @Test
    void testGetUserName() {
        given(userRepository.findNameById(1L)).willReturn("Alice");
        System.out.println(userService.getUserName(1L));
    }
}

@Service
class UserService {
    @Autowired
    private UserRepository userRepository;

    public String getUserName(Long id) {
        return userRepository.findNameById(id);
    }
}

interface UserRepository {
    String findNameById(Long id);
}
Anull
BAlice
CThrows NullPointerException
DCompilation error due to missing bean
Attempts:
2 left
💡 Hint

Think about what @MockBean does and how given(...).willReturn(...) affects the mock.

assertion
intermediate
2:00remaining
Which assertion correctly verifies a mocked method call with @MockBean?

You have a @MockBean for PaymentService. Which assertion correctly verifies that processPayment() was called exactly once?

JUnit
import static org.mockito.Mockito.*;

@MockBean
PaymentService paymentService;

// In test method:
paymentService.processPayment(100);
Averify(paymentService, times(1)).processPayment(100);
BassertEquals(1, paymentService.processPayment(100));
CassertTrue(paymentService.processPayment(100));
Dverify(paymentService).processPayment(anyInt());
Attempts:
2 left
💡 Hint

Remember how Mockito verifies method calls on mocks.

🔧 Debug
advanced
2:00remaining
Why does this test fail with NullPointerException despite using @MockBean?

Given this test, why does it throw a NullPointerException?

JUnit
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import static org.mockito.BDDMockito.given;

@SpringBootTest
class OrderServiceTest {

    @MockBean
    private PaymentService paymentService;

    private OrderService orderService = new OrderService();

    @Test
    void testOrder() {
        given(paymentService.pay(50)).willReturn(true);
        boolean result = orderService.placeOrder(50);
        System.out.println(result);
    }
}

class OrderService {
    private PaymentService paymentService;

    public boolean placeOrder(int amount) {
        return paymentService.pay(amount);
    }
}

interface PaymentService {
    boolean pay(int amount);
}
AOrderService.paymentService is null because it is not injected by Spring
BThe @MockBean annotation is missing on OrderService
Cgiven() is called on a null object causing NullPointerException
DSpringBootTest does not support @MockBean in this context
Attempts:
2 left
💡 Hint

Check how the OrderService instance is created and how dependencies are injected.

🧠 Conceptual
advanced
2:00remaining
What is the main difference between @Mock and @MockBean in Spring tests?

Choose the best explanation of the difference between @Mock and @MockBean in Spring Boot testing.

A@MockBean automatically verifies method calls; @Mock requires manual verification
B@MockBean is used only for integration tests; @Mock is used only for unit tests
C@MockBean creates a spy object; @Mock creates a pure mock
D@Mock creates a mock without Spring context integration; @MockBean creates a mock and injects it into the Spring context
Attempts:
2 left
💡 Hint

Think about how Spring manages beans and how mocks are injected.

framework
expert
2:00remaining
Which Spring test configuration ensures @MockBean mocks are injected properly in nested test classes?

You have nested test classes inside a Spring Boot test. Which configuration ensures @MockBean mocks are injected correctly in all nested classes?

AUse @SpringBootTest only on nested classes and @MockBean only on the outer class
BAnnotate each nested test class separately with @SpringBootTest and @MockBean
CAnnotate the outer test class with @SpringBootTest and use @TestInstance(Lifecycle.PER_CLASS)
DUse @MockBean only in the outer class and annotate nested classes with @ExtendWith(MockitoExtension.class)
Attempts:
2 left
💡 Hint

Consider how Spring manages context and test instance lifecycle.