|
| 1 | +package com.baeldung.spring.notamanagedtype; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.springframework.boot.SpringApplication.run; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.springframework.context.ConfigurableApplicationContext; |
| 9 | + |
| 10 | +import com.baeldung.spring.notamanagedtype.missedannotation.EntityWithoutAnnotationApplication; |
| 11 | +import com.baeldung.spring.notamanagedtype.missedannotationfixed.EntityWithoutAnnotationFixedApplication; |
| 12 | +import com.baeldung.spring.notamanagedtype.missedannotationfixed.EntityWithoutAnnotationFixedRepository; |
| 13 | +import com.baeldung.spring.notamanagedtype.jakartaannotation.EntityWithJakartaAnnotationApplication; |
| 14 | +import com.baeldung.spring.notamanagedtype.missedentityscan.app.WrongEntityScanApplication; |
| 15 | +import com.baeldung.spring.notamanagedtype.missedentityscan.fixed.app.WrongEntityScanFixedApplication; |
| 16 | +import com.baeldung.spring.notamanagedtype.missedentityscan.repository.CorrectEntityRepository; |
| 17 | + |
| 18 | +class NotManagedTypeExceptionIntegrationTest { |
| 19 | + @Test |
| 20 | + void givenEntityWithoutAnnotationApplication_whenBootstrap_thenExpectedExceptionThrown() { |
| 21 | + Exception exception = assertThrows(Exception.class, |
| 22 | + () -> run(EntityWithoutAnnotationApplication.class)); |
| 23 | + |
| 24 | + assertThat(exception) |
| 25 | + .getRootCause() |
| 26 | + .hasMessageContaining("Not a managed type"); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void givenEntityWithoutAnnotationApplicationFixed_whenBootstrap_thenRepositoryBeanShouldBePresentInContext() { |
| 31 | + ConfigurableApplicationContext context = run(EntityWithoutAnnotationFixedApplication.class); |
| 32 | + EntityWithoutAnnotationFixedRepository repository = context |
| 33 | + .getBean(EntityWithoutAnnotationFixedRepository.class); |
| 34 | + |
| 35 | + assertThat(repository).isNotNull(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void givenEntityWithJakartaAnnotationApplication_whenBootstrap_thenExpectedExceptionThrown() { |
| 40 | + Exception exception = assertThrows(Exception.class, |
| 41 | + () -> run(EntityWithJakartaAnnotationApplication.class)); |
| 42 | + |
| 43 | + assertThat(exception) |
| 44 | + .getRootCause() |
| 45 | + .hasMessageContaining("Not a managed type"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void givenWrongEntityScanApplication_whenBootstrap_thenExpectedExceptionThrown() { |
| 50 | + Exception exception = assertThrows(Exception.class, |
| 51 | + () -> run(WrongEntityScanApplication.class)); |
| 52 | + |
| 53 | + assertThat(exception) |
| 54 | + .getRootCause() |
| 55 | + .hasMessageContaining("Not a managed type"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void givenWrongEntityScanApplicationFixed_whenBootstrap_thenRepositoryBeanShouldBePresentInContext() { |
| 60 | + ConfigurableApplicationContext context = run(WrongEntityScanFixedApplication.class); |
| 61 | + CorrectEntityRepository repository = context |
| 62 | + .getBean(CorrectEntityRepository.class); |
| 63 | + |
| 64 | + assertThat(repository).isNotNull(); |
| 65 | + } |
| 66 | +} |
0 commit comments