Java Junit:测试父子实体
我有一份实体菜单,有一家儿童关系餐厅。我将检查是否有餐厅提供菜单,菜单无法删除,因此我进行了以下Junit测试:Java Junit:测试父子实体,java,jpa,junit,spring-data-jpa,spring-data,Java,Jpa,Junit,Spring Data Jpa,Spring Data,我有一份实体菜单,有一家儿童关系餐厅。我将检查是否有餐厅提供菜单,菜单无法删除,因此我进行了以下Junit测试: Restaurant resto = new Restaurant(menu); restaurantService.save(resto); menuService.delete (menu); menu = menuService.findByMenuId(menuName); assertNotNull (
Restaurant resto = new Restaurant(menu);
restaurantService.save(resto);
menuService.delete (menu);
menu = menuService.findByMenuId(menuName);
assertNotNull (menu);
但是我当然不能测试这个用例,因为我有一个异常:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
public class Menu {
...
@OneToMany(mappedBy = "menu",
cascade = CascadeType.ALL,
orphanRemoval = true, fetch = FetchType.LAZY)
@JsonIgnore
private Set<Restaurants> restaurant = new HashSet<>();
...
}
在这种情况下,assert语句没有帮助。您需要使用“expected”来检查是否没有发生删除,是否抛出异常
@Test(expected=MySQLIntegrityConstraintViolationException.class)
public void testMenuDeletionFailure() {
\\invoke the method you need to unit test, there is no need of assertion statements
}
试试这个..你能给我们看看你的实体吗?
@Test(expected=MySQLIntegrityConstraintViolationException.class)
public void testMenuDeletionFailure() {
\\invoke the method you need to unit test, there is no need of assertion statements
}