0
0
Spring Bootframework~5 mins

@DataJpaTest for repository testing in Spring Boot - Cheat Sheet & Quick Revision

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

@DataJpaTest is used to test JPA repositories. It sets up an in-memory database and configures Spring Data JPA components for fast and focused testing of database layers.

Click to reveal answer
beginner
Which database does @DataJpaTest use by default during testing?

By default, @DataJpaTest uses an embedded in-memory database like H2, HSQL, or Derby to run tests quickly without affecting the real database.

Click to reveal answer
intermediate
Does @DataJpaTest load the full Spring Boot application context?

No, @DataJpaTest loads only the JPA-related components, not the full application context. This makes tests faster and more focused on repository behavior.

Click to reveal answer
intermediate
How can you customize the database used by @DataJpaTest?

You can customize the database by adding a test profile with your own datasource or by using @AutoConfigureTestDatabase to replace or disable the embedded database.

Click to reveal answer
beginner
What happens to data after a test annotated with @DataJpaTest finishes?

Data is rolled back after each test method by default, so tests do not affect each other and the database stays clean.

Click to reveal answer
What does @DataJpaTest primarily configure for testing?
AJPA repositories and in-memory database
BFull web server and controllers
CSecurity and authentication
DExternal API clients
Which of these is true about @DataJpaTest?
AIt loads the entire Spring Boot application context.
BIt disables JPA repositories.
CIt rolls back transactions after each test.
DIt requires a real external database.
How can you prevent @DataJpaTest from using the embedded database?
AUse <code>@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)</code>
BAdd <code>@SpringBootTest</code> annotation
CSet <code>spring.jpa.hibernate.ddl-auto=none</code>
DUse <code>@EnableWebMvc</code>
Which annotation is commonly used together with @DataJpaTest to inject repository beans?
A<code>@ComponentScan</code>
B<code>@Autowired</code>
C<code>@Service</code>
D<code>@Controller</code>
What is the main benefit of using @DataJpaTest over @SpringBootTest for repository tests?
AAutomatically generates test data
BRuns tests on production database
CIncludes UI components for testing
DFaster tests by loading only JPA components
Explain how @DataJpaTest helps in testing Spring Data JPA repositories.
Think about what parts of the app it loads and how it handles data.
You got /4 concepts.
    Describe how to customize the database used in @DataJpaTest and why you might want to do that.
    Consider when embedded databases might not be enough.
    You got /4 concepts.