我在我的 repo 類中創建了 delete 方法,并在類級別使用了 @Transactional,
當我呼叫 deletebyID 時,它會洗掉該 ID。在 Junit 中,我確實使用了 DirtiesContext 它應該回滾事務,但是由于類級別的@Transactional,它改變了我的洗掉(它做了應該做的事情),
現在我的 juint 無法回滾該事務。
如何克服這個問題?
@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;
}
}
在我的六月
@Autowired
CourseRepository courseRepo;
@Test
@DirtiesContext
public void deleteById_basicTest() {
courseRepo.deleteById(100001L);
assertNull(courseRepo.findById(100001L));
}
uj5u.com熱心網友回復:
您可以對每個測驗用例使用 @Transactional。它將回滾事務。有用。
@Test
@Transactional
public void deleteById_basicTest() {
courseRepo.deleteById(100001L);
assertNull(courseRepo.findById(100001L));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/438888.html
