|
1 | 1 | package org.baeldung.ex.dataIntegrityviolationexception; |
2 | 2 |
|
3 | 3 | import org.baeldung.ex.dataIntegrityviolationexception.spring.Cause1DataContextWithJavaConfig; |
| 4 | +import org.baeldung.persistence.model.Child; |
| 5 | +import org.baeldung.persistence.model.Parent; |
| 6 | +import org.baeldung.persistence.service.IChildService; |
| 7 | +import org.baeldung.persistence.service.IParentService; |
4 | 8 | import org.junit.Test; |
5 | 9 | import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.dao.DataIntegrityViolationException; |
6 | 12 | import org.springframework.test.context.ContextConfiguration; |
7 | 13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
8 | 14 | import org.springframework.test.context.support.AnnotationConfigContextLoader; |
|
11 | 17 | @ContextConfiguration(classes = { Cause1DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class) |
12 | 18 | public class Cause1DataIntegrityViolationExceptionIntegrationTest { |
13 | 19 |
|
14 | | - @Test |
15 | | - public final void givenContextIsInitialized_thenNoException() { |
16 | | - // |
| 20 | + @Autowired |
| 21 | + private IParentService service; |
| 22 | + |
| 23 | + @Autowired |
| 24 | + private IChildService childService; |
| 25 | + |
| 26 | + // tests |
| 27 | + |
| 28 | + @Test(expected = DataIntegrityViolationException.class) |
| 29 | + public void whenChildIsDeletedWhileParentStillHasForeignKeyToIt_thenDataException() { |
| 30 | + final Child childEntity = new Child(); |
| 31 | + childService.create(childEntity); |
| 32 | + |
| 33 | + final Parent parentEntity = new Parent(childEntity); |
| 34 | + service.create(parentEntity); |
| 35 | + |
| 36 | + childService.delete(childEntity); |
17 | 37 | } |
18 | 38 |
|
19 | 39 | } |
0 commit comments