不知道大家在用spring做单元测试时,有没有遇上如下异常信息org.springframework.transaction.UnexpectedRollbackException: Transaction has been rolled back because it has been marked as roll
其实我定单元测试时和往常一样,只是创建的数据顺序和删除的顺序不一致导致的,后来调整,问题就解决了
代码示例:
// create operation
User user = new User();
userService.create(user);
Book book = new Book();
bookService.create(book);
// delete operation
bookService.delete(book);
userService.delete(user);
上面的黑体代码将会出现该异常
而调整为:
userService.delete(user);
bookService.delete(book);
后,执行单元测试成功:)