Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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?

Practice

(1/5)
1. What is the main purpose of using @DataJpaTest in Spring Boot?
easy
A. To test JPA repositories with an in-memory database setup
B. To test the entire Spring Boot application including web layer
C. To configure security settings for the application
D. To run integration tests with external APIs

Solution

  1. Step 1: Understand the role of @DataJpaTest

    @DataJpaTest is designed to test only the JPA repository layer, not the full application.
  2. Step 2: Recognize the default setup

    It sets up an in-memory database automatically to test repository methods quickly and in isolation.
  3. Final Answer:

    To test JPA repositories with an in-memory database setup -> Option A
  4. Quick Check:

    @DataJpaTest = repository testing with in-memory DB [OK]
Hint: Remember @DataJpaTest focuses only on JPA repositories [OK]
Common Mistakes:
  • Thinking it tests the full application context
  • Confusing it with @SpringBootTest
  • Assuming it configures web or security layers
2. Which of the following is the correct way to annotate a test class to use @DataJpaTest?
easy
A. @RepositoryTest public class MyRepositoryTest {}
B. @SpringBootTest public class MyRepositoryTest {}
C. @DataJpaTest public class MyRepositoryTest {}
D. @EnableJpaRepositories public class MyRepositoryTest {}

Solution

  1. Step 1: Identify the correct annotation for JPA repository testing

    @DataJpaTest is the dedicated annotation for testing JPA repositories.
  2. Step 2: Check the syntax usage

    The annotation is placed directly above the test class declaration as shown in @DataJpaTest public class MyRepositoryTest {}.
  3. Final Answer:

    @DataJpaTest public class MyRepositoryTest {} -> Option C
  4. Quick Check:

    Correct annotation usage = @DataJpaTest public class MyRepositoryTest {} [OK]
Hint: Use @DataJpaTest directly on the test class [OK]
Common Mistakes:
  • Using @SpringBootTest instead of @DataJpaTest
  • Using non-existent @RepositoryTest annotation
  • Confusing configuration annotations with test annotations
3. Given the following repository test code using @DataJpaTest, what will be the output of System.out.println(users.size())?
@DataJpaTest
public class UserRepositoryTest {
  @Autowired
  private UserRepository userRepository;

  @Test
  void testSaveAndFind() {
    User user = new User(null, "Alice");
    userRepository.save(user);
    List<User> users = userRepository.findAll();
    System.out.println(users.size());
  }
}
medium
A. 1
B. 0
C. NullPointerException
D. Compilation error

Solution

  1. Step 1: Analyze the test method behavior

    The test saves one User entity to the repository, then fetches all users.
  2. Step 2: Understand @DataJpaTest behavior

    @DataJpaTest uses an in-memory database, so the saved user is stored and retrievable within the test.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Saved one user, findAll returns list size 1 [OK]
Hint: Saved entities appear in findAll during @DataJpaTest [OK]
Common Mistakes:
  • Assuming the database is empty after save
  • Expecting NullPointerException due to missing ID
  • Confusing @DataJpaTest with no database setup
4. You wrote a test with @DataJpaTest but it fails with a BeanCreationException related to missing DataSource. What is the most likely cause?
medium
A. You did not annotate the test class with @SpringBootTest
B. You forgot to include an in-memory database dependency like H2
C. You used @DataJpaTest on a non-repository class
D. You did not add @Autowired to the repository field

Solution

  1. Step 1: Understand the error cause

    BeanCreationException for DataSource usually means Spring cannot find a database connection.
  2. Step 2: Check dependencies for @DataJpaTest

    @DataJpaTest uses an in-memory database by default, but you must include a dependency like H2 in your project.
  3. Final Answer:

    You forgot to include an in-memory database dependency like H2 -> Option B
  4. Quick Check:

    Missing DB dependency causes DataSource error [OK]
Hint: Add H2 dependency for @DataJpaTest to create DataSource [OK]
Common Mistakes:
  • Confusing @DataJpaTest with @SpringBootTest requirement
  • Ignoring missing database dependencies
  • Assuming @Autowired omission causes DataSource error
5. You want to test a repository method that uses a custom query with @Query annotation. How can you ensure @DataJpaTest loads this method correctly and your test verifies it?
hard
A. Add @EnableJpaRepositories to the test class to enable custom queries
B. Use @SpringBootTest instead because @DataJpaTest does not support custom queries
C. Manually configure the EntityManager in the test to run the query
D. Write a test method in a @DataJpaTest class that calls the custom query method and asserts the result

Solution

  1. Step 1: Understand @DataJpaTest capabilities

    @DataJpaTest loads repository beans including custom query methods annotated with @Query.
  2. Step 2: Write a test method to call and assert the custom query

    Simply call the repository method in your test and check the returned data matches expectations.
  3. Final Answer:

    Write a test method in a @DataJpaTest class that calls the custom query method and asserts the result -> Option D
  4. Quick Check:

    @DataJpaTest supports custom queries; test them directly [OK]
Hint: Test custom queries directly in @DataJpaTest methods [OK]
Common Mistakes:
  • Thinking @DataJpaTest cannot run custom queries
  • Overcomplicating with manual EntityManager setup
  • Adding unnecessary annotations like @EnableJpaRepositories
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard