0
0
Spring Bootframework~10 mins

@DataJpaTest for repository testing in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to annotate the test class for JPA repository testing.

Spring Boot
@[1] 
public class UserRepositoryTest {
    // test methods
}
Drag options to blanks, or click blank then click option'
ADataJpaTest
BSpringBootTest
CRestController
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @SpringBootTest instead of @DataJpaTest
Forgetting the @DataJpaTest annotation
2fill in blank
medium

Complete the code to inject the repository into the test class.

Spring Boot
@Autowired
private [1] userRepository;
Drag options to blanks, or click blank then click option'
AUserService
BUserRepository
CUserController
DUserEntity
Attempts:
3 left
💡 Hint
Common Mistakes
Injecting a service or controller instead of the repository
Using the entity class instead of the repository
3fill in blank
hard

Fix the error in the test method to save and verify a user entity.

Spring Boot
@Test
void testSaveUser() {
    User user = new User();
    user.setName("Alice");
    userRepository.[1](user);
    assertNotNull(user.getId());
}
Drag options to blanks, or click blank then click option'
Asave
Bdelete
CfindAll
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete or findAll instead of save
Trying to call update which does not exist in JpaRepository
4fill in blank
hard

Fill both blanks to write a query method test that finds a user by name.

Spring Boot
@Test
void testFindByName() {
    User user = new User();
    user.setName("Bob");
    userRepository.save(user);
    Optional<User> found = userRepository.[1]("Bob");
    assertTrue(found.[2]());
}
Drag options to blanks, or click blank then click option'
AfindByName
BisPresent
Cget
DexistsByName
Attempts:
3 left
💡 Hint
Common Mistakes
Using get() without checking presence
Using existsByName which returns boolean, not Optional
5fill in blank
hard

Fill all three blanks to create a test that deletes a user and verifies it no longer exists.

Spring Boot
@Test
void testDeleteUser() {
    User user = new User();
    user.setName("Carol");
    userRepository.save(user);
    userRepository.[1](user);
    Boolean exists = userRepository.[2](user.getId());
    assertFalse(exists.[3]());
}
Drag options to blanks, or click blank then click option'
Adelete
BexistsById
CbooleanValue
DdeleteById
Attempts:
3 left
💡 Hint
Common Mistakes
Using deleteById instead of delete when passing the entity
Not converting Boolean to primitive before assertFalse