public interface UserRepository extends JPARepository<User, Integer> {
@Modifying
@Query("update User u set u.active = false where u.userId = :userId")
void updateEmployee(Integer userId);
.......
}
I am new to unit testing. So I am trying to write test cases for JPQL using DataJpaTest for the above repository method. Every time my test case failed when I used the following method to test also I could not identify where I was making mistakes. If anyone has a solution it would be really helpful to me. Thanks in Advance.!
In the test case, I wrote, that the field active does not get updated and The value is also null.
@Test
void updateUserRecord(){
User user = new User();
user.setUserId(1);
userRepository.save(user);
userRepository.updateEmployee(user.getUserId());
assertFalse(user.getActive());
}
When using @Modifying, @Query annotation, JPA directly update database. Not update persistent context. So your user instance in test code is not updated. You have to re-fetch from database.
I think using entity manager as a helper makes your test code better.
flushmethod reflects entities changes in persistent context to database.clearmethod clears persistent context.