|
| 1 | +package org.baeldung.persistence.service; |
| 2 | + |
| 3 | +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; |
| 4 | + |
| 5 | +import org.baeldung.persistence.model.Foo; |
| 6 | +import org.baeldung.spring.PersistenceConfig; |
| 7 | +import org.junit.Ignore; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.dao.DataIntegrityViolationException; |
| 12 | +import org.springframework.dao.InvalidDataAccessApiUsageException; |
| 13 | +import org.springframework.test.context.ContextConfiguration; |
| 14 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 15 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 16 | + |
| 17 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 18 | +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) |
| 19 | +public class FooServicePersistenceIntegrationTest { |
| 20 | + |
| 21 | + @Autowired |
| 22 | + private IFooService service; |
| 23 | + |
| 24 | + // tests |
| 25 | + |
| 26 | + @Test |
| 27 | + public final void whenContextIsBootstrapped_thenNoExceptions() { |
| 28 | + // |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public final void whenEntityIsCreated_thenNoExceptions() { |
| 33 | + service.create(new Foo(randomAlphabetic(6))); |
| 34 | + } |
| 35 | + |
| 36 | + @Test(expected = DataIntegrityViolationException.class) |
| 37 | + public final void whenInvalidEntityIsCreated_thenDataException() { |
| 38 | + service.create(new Foo()); |
| 39 | + } |
| 40 | + |
| 41 | + @Test(expected = DataIntegrityViolationException.class) |
| 42 | + public final void whenEntityWithLongNameIsCreated_thenDataException() { |
| 43 | + service.create(new Foo(randomAlphabetic(2048))); |
| 44 | + } |
| 45 | + |
| 46 | + @Test(expected = InvalidDataAccessApiUsageException.class) |
| 47 | + @Ignore("Right now, persist has saveOrUpdate semantics, so this will no longer fail") |
| 48 | + public final void whenSameEntityIsCreatedTwice_thenDataException() { |
| 49 | + final Foo entity = new Foo(randomAlphabetic(8)); |
| 50 | + service.create(entity); |
| 51 | + service.create(entity); |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments