0
0
JUnittesting~5 mins

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

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

@MockBean creates and injects a mock of a Spring bean into 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 Mockito's @Mock annotation?

@MockBean integrates with Spring's application context and replaces beans there, while @Mock creates a mock object but does not replace Spring beans automatically.

Click to reveal answer
beginner
Where should you place the @MockBean annotation in a Spring Boot test?
<p>You place <code>@MockBean</code> on a field in your test class where you want Spring to inject the mock bean instead of the real one.</p>
Click to reveal answer
beginner
Can @MockBean be used to mock multiple beans in the same test?
<p>Yes, you can declare multiple <code>@MockBean</code> fields in the same test class to mock different beans as needed.</p>
Click to reveal answer
intermediate
What happens if you use @MockBean on a bean that is not present in the Spring context?

Spring will add the mock bean to the context, so it can be used by other beans or the test, even if the original bean was not defined.

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 affect Spring beans
CRuns the real bean without mocking
DDisables Spring context loading
Where do you declare @MockBean in your test code?
AInside the bean class
BOn the main application class
COn a field in the test class
DIn the application.properties file
Which of these is a key benefit of using @MockBean over @Mock?
AIt disables all beans except mocks
BIt runs tests faster by skipping mocks
CIt automatically generates test data
DIt integrates mocks into Spring's application context
Can you use multiple @MockBean annotations in one test class?
AOnly in integration tests
BYes, to mock multiple beans
COnly if beans are of the same type
DNo, only one mock bean per test
What happens if @MockBean is used on a bean not originally in the Spring context?
ASpring adds the mock bean to the context
BTest fails to start
CMock is ignored
DSpring throws an error
Explain how @MockBean helps isolate units in Spring Boot tests.
Think about how mocking helps focus on one part of the code.
You got /3 concepts.
    Describe the difference between @MockBean and @Mock in Spring testing.
    Consider how Spring manages beans versus plain mocks.
    You got /3 concepts.