Correctness17 - SpotBugsExtensionForSpringFrameWork/CS5098 GitHub Wiki
(Suitable) Bug Name
Description
// Failure code
@Test(expected = DataIntegrityViolationException.class)
public void whenChildIsDeletedWhileParentStillHasForeignKeyToIt_thenDataException() {
Child childEntity = new Child();
childService.create(childEntity);
Parent parentEntity = new Parent(childEntity); // explicit foreign key injection
service.create(parentEntity);
// this breaks the foreign key constraint on the Parent
childService.delete(childEntity); // ConstraintViolationException occur
}
Solutions
The Parent should be deleted first.
@Test
public void whenChildIsDeletedAfterTheParent_thenNoExceptions() {
Child childEntity = new Child();
childService.create(childEntity);
Parent parentEntity = new Parent(childEntity);
service.create(parentEntity);
service.delete(parentEntity);
childService.delete(childEntity);
}