- Invade the Code π½
- Posts
- π Day 18 of #100DaysOfCode: Deep Dive into JPA Entity Testing π
π Day 18 of #100DaysOfCode: Deep Dive into JPA Entity Testing π
Hey Invaders! π½
#100daysofcode day 18: I wrapped up section 13 on Testing JPA Entities. Today, I focused on crafting precise repository tests for critical functions in my UsersRepository such as .findByEmail()
and .findByUserId()
. This kind of testing is crucial for ensuring our data access layer works flawlessly, providing reliable queries and interactions with the database.
Hereβs a snippet showcasing the tests I developed:
@DataJpaTest
public class UsersRepositoryTest {
@Autowired
TestEntityManager testEntityManager;
@Autowired
UsersRepository usersRepository;
private final String userId1 = UUID.randomUUID().toString();
private final String userId2 = UUID.randomUUID().toString();
private final String email1 = "[email protected]";
private final String email2 = "[email protected]";
@BeforeEach
void setup() {
// Creating first user
UserEntity userEntity = new UserEntity();
userEntity.setUserId(userId1);
userEntity.setEmail(email1);
userEntity.setEncryptedPassword("12345678");
userEntity.setFirstName("Sergey");
userEntity.setLastName("Kargopolov");
testEntityManager.persistAndFlush(userEntity);
// Creating second user
UserEntity userEntity2 = new UserEntity();
userEntity2.setUserId(userId2);
userEntity2.setEmail(email2);
userEntity2.setEncryptedPassword("abcdefg1");
userEntity2.setFirstName("John");
userEntity2.setLastName("Sears");
testEntityManager.persistAndFlush(userEntity2);
}
@Test
void testFindByEmail_whenGivenCorrectEmail_returnsUserEntity(){
// act
UserEntity storedUser = usersRepository.findByEmail(email1);
// assert
assertEquals(email1, storedUser.getEmail(), "returned email addy does not match the expected valye");
}
@Test
void testFindByUserId_whenCorrectUserIdIsGiven_returnsUserEntity(){
UserEntity storedUser = usersRepository.findByUserId(userId2);
assertEquals(userId2, storedUser.getUserId(), "Returned User id does NOT match");
}
@Test
void testFindUsersWithEmailEndsWith_whenGivenEmailDomain_returnsUserWithGivenDomain(){
//arrange
UserEntity userEntity3 = new UserEntity();
userEntity3.setUserId(UUID.randomUUID().toString());
userEntity3.setEmail("[email protected]");
userEntity3.setEncryptedPassword("abcdefg1");
userEntity3.setFirstName("John");
userEntity3.setLastName("AppleSeed");
testEntityManager.persistAndFlush(userEntity3);
//act
String emailDomainName = "@gmail.com";
//assert
List<UserEntity> users = usersRepository.findUsersWithEmailEndingWith(emailDomainName);
assertEquals(1, users.size(), "There should be only one user in the list");
assertTrue(users.get(0).getEmail().endsWith(emailDomainName));
}
}
Importance of Annotations and JPA Testing:
@DataJpaTest: This annotation focuses on JPA components, making it ideal for testing JPA repositories or JPA slices of the application. It configures an in-memory database, scans for @Entity classes, and configures Spring Data JPA repositories. π οΈ
TestEntityManager: This is a simplified API for JPA provided by Spring Boot which is particularly useful for writing tests. It allows straightforward setup and teardown operations for test data. π
@Autowired: Enables dependency injection to wire the test context, simplifying test setup. π§©
These tests are fundamental for validating the behavior of our data access layer, ensuring that each method behaves as expected under various scenarios. By covering these critical functions through automated tests, we safeguard our application against data-related bugs and maintain data integrity, which is especially important in transaction-heavy or data-sensitive applications. β
Reply