0
0
Spring Bootframework~20 mins

@DataJpaTest for repository testing in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DataJpaTest Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does @DataJpaTest do in a Spring Boot test?
Consider a Spring Boot test class annotated with @DataJpaTest. What is the main behavior of this annotation during test execution?
AIt configures an in-memory database, scans for JPA repositories, and disables full auto-configuration.
BIt loads the entire Spring Boot application context including web server and all beans.
CIt only loads the web layer components like controllers and filters.
DIt disables all database access and mocks repository beans automatically.
Attempts:
2 left
💡 Hint
Think about what is needed to test JPA repositories without starting the full app.
state_output
intermediate
2:00remaining
What is the state of the database after a @DataJpaTest method completes?
In a test class annotated with @DataJpaTest, what happens to the database state after each test method finishes?
AThe database is committed and changes are permanently saved after each test.
BThe database is rolled back to the initial state before the test method ran.
CThe database keeps all changes made during the test method for the next test.
DThe database is dropped and recreated only once after all tests finish.
Attempts:
2 left
💡 Hint
Think about test isolation and how Spring manages transactions in tests.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly injects a repository in a @DataJpaTest class?
Given a repository interface UserRepository, which of the following code snippets correctly injects it into a test class annotated with @DataJpaTest?
A
@DataJpaTest
class UserRepositoryTest {
  @Resource
  private UserRepository userRepository;
}
B
@DataJpaTest
class UserRepositoryTest {
  @Inject
  private UserRepository userRepository;
}
C
@DataJpaTest
class UserRepositoryTest {
  private UserRepository userRepository = new UserRepository();
}
D
@DataJpaTest
class UserRepositoryTest {
  @Autowired
  private UserRepository userRepository;
}
Attempts:
2 left
💡 Hint
Which annotation is most commonly used in Spring Boot tests for dependency injection?
🔧 Debug
advanced
2:00remaining
Why does this @DataJpaTest fail with NoSuchBeanDefinitionException?
A test class annotated with @DataJpaTest tries to inject MyService which depends on UserRepository. The test fails with NoSuchBeanDefinitionException for MyService. Why?
Spring Boot
@DataJpaTest
class MyServiceTest {
  @Autowired
  private MyService myService;
}
ABecause @DataJpaTest only scans repository beans, not service beans like MyService.
BBecause MyService is not annotated with @Repository, so Spring ignores it.
CBecause @DataJpaTest disables all dependency injection by default.
DBecause MyService requires a web environment which @DataJpaTest does not provide.
Attempts:
2 left
💡 Hint
Think about what beans @DataJpaTest loads automatically.
🧠 Conceptual
expert
3:00remaining
How to include custom configuration beans in a @DataJpaTest?
You want to add a custom bean MyCustomBean to the Spring context during a @DataJpaTest. Which approach correctly adds this bean without loading the full application context?
AAdd @ComponentScan(basePackages = "com.example") on the test class.
BAdd @SpringBootTest instead of @DataJpaTest to load all beans.
CUse @Import(MyCustomBean.class) on the test class alongside @DataJpaTest.
DManually instantiate MyCustomBean inside each test method.
Attempts:
2 left
💡 Hint
How to add specific beans to a sliced test context?