0
0
Spring Bootframework~5 mins

@MockBean for mocking dependencies in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @MockBean annotation in Spring Boot tests?

@MockBean creates a mock of a Spring bean and adds it to the application context during tests. It replaces the real bean with a mock to isolate the unit under test.

Click to reveal answer
intermediate
How does @MockBean differ from using Mockito's @Mock annotation?

@MockBean integrates the mock into the Spring application context, replacing the real bean. @Mock only creates a mock object but does not replace any Spring beans.

Click to reveal answer
intermediate
Can @MockBean be used to mock multiple beans of the same type in a Spring Boot test?

Yes, you can use multiple @MockBean annotations to mock different beans, but if there are multiple beans of the same type, you may need to specify the bean name to avoid conflicts.

Click to reveal answer
beginner
Where should @MockBean be placed in a Spring Boot test class?

@MockBean is placed on fields inside the test class. Spring Boot will create and inject the mock bean into the application context before tests run.

Click to reveal answer
advanced
What happens if you use @MockBean on a bean that is not present in the application context?

Spring Boot will add the mock bean to the context even if the original bean does not exist. This can be useful to add mocks for optional dependencies.

Click to reveal answer
What does @MockBean do in a Spring Boot test?
AReplaces a Spring bean with a mock in the application context
BCreates a mock object but does not add it to the context
CRuns the real bean without mocking
DDeletes the bean from the application context
Where do you place the @MockBean annotation in your test code?
AOn the test class itself
BOn the main application class
COn fields inside the test class
DOn the method under test
Which of these is a key difference between @MockBean and Mockito's @Mock?
ABoth do the same thing
B<code>@Mock</code> replaces Spring beans, <code>@MockBean</code> does not
C<code>@MockBean</code> is only for integration tests
D<code>@MockBean</code> integrates with Spring context, <code>@Mock</code> does not
If multiple beans of the same type exist, how can you specify which one to mock with @MockBean?
AUse the <code>name</code> attribute in <code>@MockBean</code>
BYou cannot mock beans of the same type
CMock all beans automatically
DUse <code>@Mock</code> instead
What happens if @MockBean is used on a bean not defined in the Spring context?
ATest fails to start
BMock bean is added to the context anyway
CSpring ignores the annotation
DThe real bean is created instead
Explain how @MockBean helps isolate components during Spring Boot testing.
Think about how you can test one part without running the whole app.
You got /4 concepts.
    Describe the difference between @MockBean and Mockito's @Mock in Spring Boot tests.
    Consider how Spring manages beans and how mocks fit in.
    You got /4 concepts.