0
0
JUnittesting~20 mins

@DataJpaTest for repository testing in JUnit - 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!
Predict Output
intermediate
2:00remaining
What is the test execution result of this @DataJpaTest?
Consider this JUnit test using @DataJpaTest for a Spring Data JPA repository. What will be the test result?
JUnit
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
class UserRepositoryTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    void testSaveAndFind() {
        User user = new User(null, "Alice");
        userRepository.save(user);
        User found = userRepository.findByName("Alice");
        assertThat(found).isNotNull();
        assertThat(found.getName()).isEqualTo("Alice");
    }
}
ATest passes because the user is saved and found correctly in the in-memory database.
BTest fails with NullPointerException because userRepository is not injected.
CTest fails with DataIntegrityViolationException due to missing @Transactional annotation.
DTest fails because @DataJpaTest disables repository beans by default.
Attempts:
2 left
💡 Hint
Remember that @DataJpaTest configures an in-memory database and injects repository beans automatically.
assertion
intermediate
1:30remaining
Which assertion correctly verifies the repository returns no users initially?
Given a UserRepository with method findAll(), which assertion correctly checks that the repository is empty before any save?
JUnit
import static org.junit.jupiter.api.Assertions.*;

List<User> users = userRepository.findAll();
AassertNotNull(users); assertEquals(0, users.size());
BassertEquals(null, users);
CassertTrue(users.isEmpty());
DassertFalse(users.isEmpty());
Attempts:
2 left
💡 Hint
Check if the list returned by findAll() contains any elements.
🔧 Debug
advanced
2:30remaining
Why does this @DataJpaTest fail with NoSuchBeanDefinitionException?
This test fails with NoSuchBeanDefinitionException for UserRepository. What is the likely cause?
JUnit
@DataJpaTest
class UserRepositoryTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    void testFind() {
        User user = userRepository.findByName("Bob");
        assertNull(user);
    }
}
AUserRepository interface is missing @Repository annotation.
BThe test class is missing @SpringBootTest annotation to load the full context.
CThe test method is missing @Transactional annotation.
DUserRepository is not in the same package or a subpackage of the test class, so it is not scanned.
Attempts:
2 left
💡 Hint
Check the package structure and component scanning rules of @DataJpaTest.
🧠 Conceptual
advanced
1:30remaining
What is the default database used by @DataJpaTest?
When running a test annotated with @DataJpaTest without any database configuration, which database is used by default?
AAn embedded in-memory H2 database
BThe production MySQL database configured in application.properties
CAn embedded in-memory Derby database
DNo database is used; repositories are mocked automatically
Attempts:
2 left
💡 Hint
Spring Boot auto-configures an embedded database for tests if available.
framework
expert
2:30remaining
Which annotation combination ensures @DataJpaTest rolls back transactions after each test?
You want each test method in a @DataJpaTest class to run in a transaction that rolls back after the test completes. Which annotation setup achieves this?
A@DataJpaTest with @Commit to commit transactions after tests
B@DataJpaTest alone (default rollback behavior enabled)
C@DataJpaTest with @Transactional(propagation = Propagation.NOT_SUPPORTED)
D@DataJpaTest with @Rollback(false) to disable rollback
Attempts:
2 left
💡 Hint
Check the default transaction behavior of @DataJpaTest.