0
0
JUnittesting~5 mins

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

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

@DataJpaTest is used to test JPA repositories. It configures an in-memory database, scans for @Entity classes, and sets up Spring Data JPA components for testing repository layers only.

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

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 components related to JPA repositories and entities. It does not load web layers or other beans, making tests faster and focused.

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

You can override the default in-memory database by providing your own application.properties or application.yml file with datasource settings, or by using @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) to use the real database.

Click to reveal answer
beginner
What is a typical assertion you would write in a repository test using @DataJpaTest?

You typically assert that repository methods return expected data. For example, after saving an entity, you assert that findById() returns the saved entity, or that count() returns the correct number.

Click to reveal answer
What does @DataJpaTest automatically configure for your test?
ANo database, you must configure it manually
BThe full Spring Boot application context
COnly the web layer components
DAn embedded in-memory database and JPA repositories
Which annotation can you use to prevent @DataJpaTest from replacing your database with an in-memory one?
A@MockBean
B@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
C@SpringBootTest
D@Transactional
True or False: @DataJpaTest loads web controllers for testing.
AFalse
BTrue
COnly if specified
DDepends on Spring Boot version
What is the default transaction behavior of tests annotated with @DataJpaTest?
ATransactions must be managed manually
BNo transactions are used
CEach test runs in a transaction that rolls back after completion
DTransactions commit automatically after each test
Which of the following is NOT a benefit of using @DataJpaTest?
ATesting the full application including UI and services
BFaster tests by loading only repository-related beans
CAutomatic rollback of transactions after each test
DUsing an embedded in-memory database by default
Explain how @DataJpaTest helps in testing Spring Data JPA repositories.
Think about what parts of the application are loaded and how the database is handled.
You got /4 concepts.
    Describe how to configure @DataJpaTest to use your real database instead of the default in-memory one.
    Consider annotations and configuration files.
    You got /3 concepts.