I have create delete method in my repo class and did use @Transactional at class level,
When I call deletebyID its deleting that id. in Junit I did use DirtiesContext it should roll back the transaction, but due to @Transactional at class level, it commuted my delete (It did what suppose to do),
now my juint not able to rollback that transaction.
How to overcome this issue?
@Repository
@Transactional
public class CourseRepository {
@Autowired
EntityManager em;
public Course findById(Long id) {
return em.find(Course.class, id);
}
public Course deleteById(Long id) {
Course course= em.find(Course.class, id);
em.remove(course);
return course;
}
}
In my Junit
@Autowired
CourseRepository courseRepo;
@Test
@DirtiesContext
public void deleteById_basicTest() {
courseRepo.deleteById(100001L);
assertNull(courseRepo.findById(100001L));
}
CodePudding user response:
You can use @Transactional for each test case. It will rollbacks transactions. It works.
@Test
@Transactional
public void deleteById_basicTest() {
courseRepo.deleteById(100001L);
assertNull(courseRepo.findById(100001L));
}