JPA 양방향 연관관계 테스트하기

savannah030·2022년 4월 18일
0

삽질

목록 보기
7/7
post-thumbnail
post-custom-banner

JPA 양방향 연관관계 매핑하고 테스트 코드 작성하는 과정에서 발생한 험난한 여정..

삽질 1

failed to replace datasource with an embedded database for tests
-> 실제 DB가 연결되지 않았기 때문

삽질 2

luvShort_schema 연결했더니 이미 video 테이블 있다고 하길래 테이블 없는 new_schema 연결했더니 또 없다고..
-> application.yml 설정하는 거 깜빡했음.. jpa: hibernate: ddl-auto: update로 설정!

삽질 3

not-null property references a null or transient value
-> 엔티티 저장 시 임베디드 타입을 초기화시키지 않는 경우 발생

삽질 4

양방향 매핑 테스트 코드는 통과하는데 실제 DB까보면 저장 안됨.. 필드 다 안채워서 그런가?

테스트 코드 쪽 지식이 특히 부족한데 에러 뜰 때마다 구글링으로 지식 습득하고 이해하는 거 꽤 재밌다ㅋㅋ 그래도 언제 날잡고 테스트 작성하는 법 공부해야겠다

삽질 5

테스트 코드가 통과되는데도 이런 에러가 떴음

23:13:06.331 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.example.backend.domain.JpaTestApplicationTests]
23:13:06.362 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
23:13:06.398 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
23:13:06.574 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for![](https://velog.velcdn.com/images/savannah030/post/395fe824-e716-4f88-ad9e-4425111da884/image.jpg)
 test class [com.example.backend.domain.JpaTestApplicationTests] from class [org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper]
23:13:06.627 [main] INFO org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.example.backend.domain.JpaTestApplicationTests], using SpringBootContextLoader
23:13:06.641 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.backend.domain.JpaTestApplicationTests]: class path resource [com/example/backend/domain/JpaTestApplicationTests-context.xml] does not exist
23:13:06.642 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.backend.domain.JpaTestApplicationTests]: class path resource [com/example/backend/domain/JpaTestApplicationTestsContext.groovy] does not exist
23:13:06.643 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.example.backend.domain.JpaTestApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
23:13:06.645 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.example.backend.domain.JpaTestApplicationTests]: JpaTestApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
23:13:06.880 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.153 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [C:\Users\yjkim\Desktop\syhan\Spring\luvShort\backend\build\classes\java\main\com\example\backend\BackendApplication.class]
23:13:07.156 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.example.backend.BackendApplication for test class com.example.backend.domain.JpaTestApplicationTests
23:13:07.165 [main] DEBUG org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper - @TestExecutionListeners is not present for class [com.example.backend.domain.JpaTestApplicationTests]: using defaults.
23:13:07.167 [main] INFO org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
23:13:07.230 [main] INFO org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@31368b99, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1725dc0f, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@3911c2a7, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@4ac3c60d, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@4facf68f, org.springframework.test.context.support.DirtiesContextTestExecutionListener@76508ed1, org.springframework.test.context.transaction.TransactionalTestExecutionListener@41e36e46, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@15c43bd9, org.springframework.test.context.event.EventPublishingTestExecutionListener@3d74bf60, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@4f209819, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@15eb5ee5, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@2145b572, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@39529185, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@72f926e6, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@3daa422a]
23:13:07.234 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.236 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.238 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.238 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.239 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.239 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.259 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.259 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.262 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.262 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.264 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.264 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.280 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@15ff3e9e testClass = JpaTestApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@5fdcaa40 testClass = JpaTestApplicationTests, locations = '{}', classes = '{class com.example.backend.BackendApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@140e5a13, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@6c9f5c0d, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@7abc32d5, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@4e41089d, [ImportsContextCustomizer@6dc17b83 key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6b1274d2, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@ba8d91c, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map[[empty]]], class annotated with @DirtiesContext [false] with mode [null].
23:13:07.286 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.286 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.backend.domain.JpaTestApplicationTests]
23:13:07.299 [main] DEBUG org.springframework.test.context.support.DependencyInjectionTestExecutionListener - Performing dependency injection for test context [[DefaultTestContext@15ff3e9e testClass = JpaTestApplicationTests, testInstance = com.example.backend.domain.JpaTestApplicationTests@67d48005, testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@5fdcaa40 testClass = JpaTestApplicationTests, locations = '{}', classes = '{class com.example.backend.BackendApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@140e5a13, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@6c9f5c0d, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@7abc32d5, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@4e41089d, [ImportsContextCustomizer@6dc17b83 key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6b1274d2, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@ba8d91c, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]].

뭔가 잘못한 것 같기는 한데 졸리다..(스프링 환경 설정 코드 안짠듯) 자고 내일 일어나서 봐야지

테스트 통과된다면서 로그 뭐냐구...

껐다 켰더니

이런 문구가 나왔다.

해결 1

test 디렉터리에도 application.yml 작성해야하는듯하다

해결 2

원래있던 BackendApplicationTests.java를 삭제하고 JpaTestApplicationTests.java@SpringBootTest를 붙여주었다.

DB 반영됐는지 확인

Video 엔티티 만들어질 때 user_idx도 같이 저장되는 걸 볼 수 있다.

 
profile
백견이불여일타
post-custom-banner

0개의 댓글