@DataJpaTest — Concept Check
Questions on @DataJpaTest. Loads only the JPA layer, auto-configures an in-memory DB, and rolls back each test; ideal for testing repository query correctness in isolation.
Questions in this quiz
8 questions on @DataJpaTest — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.During a test using @DataJpaTest, you notice that changes made to the database are not persisting after the test completes. What is the reason for this behavior?
- AThe database is configured to be read-only during tests.
- BEach test is wrapped in a transaction that rolls back after completion.
- CThe in-memory database does not support data persistence.
- DThe @DataJpaTest annotation automatically deletes data after tests.
2.You need to test MySQL-specific window functions in your repository. Which configuration should you use?
- AUse @AutoConfigureTestDatabase(replace = NONE) with Testcontainers.
- BUse @Import to load MySQL configurations.
- CUse the default H2 in-memory database.
- DConfigure a mock database to simulate MySQL.
3.A developer believes @DataJpaTest loads all Spring components for testing. What is the correct behavior?
- AIt loads only controllers and repositories.
- BIt loads only services and @Component beans.
- CIt loads only the JPA layer, not controllers, services, or @Component beans.
- DIt loads all components including controllers and services.
4.In a @DataJpaTest, you observe that @CreatedDate fields are not being set. What might be missing in your configuration?
- AThe H2 database does not support auditing.
- BThe @CreatedDate annotation is incorrectly applied.
- C@DataJpaTest does not support auditing by default.
- D@EnableJpaAuditing configuration is not imported.
5.What is the primary advantage of using an in-memory database like H2 with @DataJpaTest?
- AH2 provides better error messages for debugging.
- BIt supports all SQL features without configuration.
- CIn-memory databases are always more accurate than real databases.
- DTests are fast and deterministic due to automatic rollback.
6.You need to ensure that your JPA converters are tested. How does @DataJpaTest help with this?
- AIt disables converters to simplify testing.
- BIt requires manual configuration of converters.
- CIt includes @Converters and auditing infrastructure by default.
- DIt only tests repository methods, not converters.
7.What happens if you run a @DataJpaTest without any additional database configuration?
- AIt defaults to using a real MySQL database.
- BIt uses an embedded H2 in-memory database by default.
- CIt uses a file-based database for persistence.
- DIt fails due to lack of database configuration.
8.A developer claims that @DataJpaTest can test service layer logic. What is the correct understanding?
- AIt can test service logic if configured properly.
- BService logic tests require a separate configuration.
- C@DataJpaTest is designed to test only the JPA layer, not service logic.
- DIt automatically includes service layer tests.