Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Junit:测试父子实体_Java_Jpa_Junit_Spring Data Jpa_Spring Data - Fatal编程技术网

Java 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 (

我有一份实体菜单,有一家儿童关系餐厅。我将检查是否有餐厅提供菜单,菜单无法删除,因此我进行了以下Junit测试:

    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
}