0
0
Spring Bootframework~10 mins

@DataJpaTest for repository testing in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @DataJpaTest for repository testing
Start Test Class
@DataJpaTest Annotation
Spring Boot Loads In-Memory DB
Inject Repository Bean
Run Repository Methods
Verify Results
Test Ends
The test class annotated with @DataJpaTest loads an in-memory database and injects the repository to run and verify database operations.
Execution Sample
Spring Boot
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.beans.factory.annotation.Autowired;
@DataJpaTest
class UserRepositoryTest {
  @Autowired
  UserRepository repo;
  // test methods here
}
This code sets up a test class that uses @DataJpaTest to test UserRepository with an in-memory database.
Execution Table
StepActionSpring Boot BehaviorRepository StateTest Output
1Test class startsSpring Boot prepares test contextRepository bean not yet injectedNo output
2@DataJpaTest triggers in-memory DB setupH2 database startsRepository bean created and injectedNo output
3Repository method called (e.g., save)Transaction startsEntity saved in in-memory DBEntity saved successfully
4Repository method called (e.g., findById)Query executed on in-memory DBEntity retrievedEntity returned
5Assertions runTest framework checks resultsRepository state unchangedPass or Fail based on assertions
6Test endsTransaction rolls back automaticallyIn-memory DB clearedTest context closed
💡 Test ends after assertions; in-memory DB is cleared to keep tests isolated.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
Repository BeannullInjected instanceInjected instanceInjected instance
In-Memory DBemptyContains saved entityContains saved entityempty (rolled back)
EntitynullSaved entity instanceRetrieved entity instancenull (rolled back)
Key Moments - 3 Insights
Why does the database get cleared after the test?
Because @DataJpaTest runs each test in a transaction that rolls back at the end, ensuring no data persists between tests (see Step 6 in execution_table).
Is the real database used during @DataJpaTest?
No, @DataJpaTest uses an in-memory database like H2 by default to keep tests fast and isolated (see Step 2 in execution_table).
How does the repository get available in the test?
Spring Boot injects the repository bean automatically into the test class because of @DataJpaTest and @Autowired (see Step 2 and 3 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at Step 3?
ARepository method saves an entity in the in-memory DB
BTest ends and database is cleared
CRepository bean is injected
DAssertions are run
💡 Hint
Check the 'Action' and 'Repository State' columns at Step 3 in the execution_table.
At which step does Spring Boot start the in-memory database?
AStep 1
BStep 2
CStep 4
DStep 6
💡 Hint
Look for 'H2 database starts' in the 'Spring Boot Behavior' column.
If the transaction did not roll back after the test, what would change in variable_tracker?
AIn-Memory DB would remain empty
BRepository Bean would be null
CIn-Memory DB would still contain saved entities after test
DEntity variable would be null at start
💡 Hint
Refer to the 'In-Memory DB' row in variable_tracker and Step 6 in execution_table.
Concept Snapshot
@DataJpaTest sets up an in-memory database and injects repository beans for testing.
Each test runs in a transaction that rolls back after completion.
This keeps tests isolated and fast without affecting real data.
Use @Autowired to get repository instances in your test class.
Run repository methods and assert results as usual.
The in-memory DB resets after each test automatically.
Full Transcript
When you write a test class with @DataJpaTest, Spring Boot starts an in-memory database like H2 and injects your repository bean. You can then call repository methods such as save or findById. Each test runs inside a transaction that automatically rolls back when the test finishes, so no data stays in the database. This keeps tests clean and isolated. The repository bean is available because Spring Boot creates and injects it for you. After running your assertions, the test ends and the in-memory database clears. This process helps you test your database code quickly without touching your real database.