인프런 커뮤니티 질문&답변
테스트 간헐적 실패에서 대해서 다루나요?
해결된 질문
작성
·
160
·
수정됨
0
안녕하세요
섹션5 코드 다듬기까지 들은 시점에서 테스트 코드를 여러번 실행했을때 간헐적으로 실패를 경험합니다
혹시 해당 내용 수정하는 부분이 끝까지 들으면 나오나요
아래 에러내용 삽입했습니다.

Failed to resolve parameter [com.clean.splearn.application.provided.MemberFinder memberFinder] in constructor [com.clean.splearn.application.provided.MemberFinderTest(com.clean.splearn.application.provided.MemberFinder,com.clean.splearn.application.provided.MemberRegister,jakarta.persistence.EntityManager)]: Failed to load ApplicationContext for [WebMergedContextConfiguration@350ec690 testClass = com.clean.splearn.application.provided.MemberFinderTest, locations = [], classes = [com.clean.splearn.SplearnApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.autoconfigure.OnFailureConditionReportContextCustomizerFactory$OnFailureConditionReportContextCustomizer@476fe690, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@6dd1c3ed, [ImportsContextCustomizer@49cb1baf key = [com.clean.splearn.SplearnTestConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@7fdab70c, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@a451491, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2aff9dff, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@15639440, org.springframework.test.context.support.DynamicPropertiesContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestAnnotation@cbfbc3c], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
org.junit.jupiter.api.extension.ParameterResolutionException: Failed to resolve parameter [com.clean.splearn.application.provided.MemberFinder memberFinder] in constructor [com.clean.splearn.application.provided.MemberFinderTest(com.clean.splearn.application.provided.MemberFinder,com.clean.splearn.application.provided.MemberRegister,jakarta.persistence.EntityManager)]: Failed to load ApplicationContext for [WebMergedContextConfiguration@350ec690 testClass = com.clean.splearn.application.provided.MemberFinderTest, locations = [], classes = [com.clean.splearn.SplearnApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.autoconfigure.OnFailureConditionReportContextCustomizerFactory$OnFailureConditionReportContextCustomizer@476fe690, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@6dd1c3ed, [ImportsContextCustomizer@49cb1baf key = [com.clean.splearn.SplearnTestConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@7fdab70c, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@a451491, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2aff9dff, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@15639440, org.springframework.test.context.support.DynamicPropertiesContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestAnnotation@cbfbc3c], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
at java.base/java.util.Optional.orElseGet(Optional.java:364)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@350ec690 testClass = com.clean.splearn.application.provided.MemberFinderTest, locations = [], classes = [com.clean.splearn.SplearnApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.autoconfigure.OnFailureConditionReportContextCustomizerFactory$OnFailureConditionReportContextCustomizer@476fe690, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@6dd1c3ed, [ImportsContextCustomizer@49cb1baf key = [com.clean.splearn.SplearnTestConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@7fdab70c, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@a451491, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2aff9dff, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@15639440, org.springframework.test.context.support.DynamicPropertiesContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestAnnotation@cbfbc3c], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:180)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:130)
at org.springframework.test.context.junit.jupiter.SpringExtension.getApplicationContext(SpringExtension.java:351)
at org.springframework.test.context.junit.jupiter.SpringExtension.resolveParameter(SpringExtension.java:337)
... 3 more
답변 3
1
1
0
안녕하세요, 인프런 AI 인턴입니다.
김코틀린자바님께서 제기하신 문제는 테스트 케이스를 여러 번 실행할 때 발생하는 간헐적인 실패와 관련된 것으로 보입니다. 주로 테스트 환경 설정이나 의존성 주입 관련 문제에서 발생할 수 있습니다. 이러한 문제를 해결하기 위한 다양한 방법들이 있으며, 스프링에서의 다양한 설정이나 도구, 전략을 통해 이를 처리하는 방법이 있습니다.
테스트에서 간헐적으로 발생하는 실패는 다양한 원인으로부터 발생할 수 있으며, 여기서는 몇 가지 중요한 테스트 관련 사항을 다루고자 합니다.
ApplicationContext 로딩 문제: 스프링 부트 테스트에서 ApplicationContext가 정상적으로 로딩되지 않으면 테스트가 실패할 수 있습니다. 특히,
@SpringBootTest를 사용할 때 의존성 주입이 잘못되거나, 빈(BEAN)이 등록되지 않으면 이와 같은 오류가 발생할 수 있습니다. 주입하려는 빈이 올바르게 설정되었는지, 관련된 설정이 올바른지 확인해야 합니다.테스트 환경의 격리: 테스트 간에 데이터베이스나 캐시가 공유되는 경우 문제가 발생할 수 있습니다. 테스트 케이스 간에 영향을 미치지 않도록
@DirtiesContext나@Transactional을 사용해보세요. 이는 각 테스트가 깨끗한 상태에서 시작되도록 도와줍니다.의존성 주입 문제: 느슨한 의존성 분리와 명확한 bean 설정이 중요합니다. 테스트 클래스에서
@MockBean을 사용하여 필요한 의존성을 모의(Mock) 객체로 설정하거나, 실제 상호작용에 문제가 있는 객체들을 모킹(Mocking)하는 것도 좋은 방법입니다.테스트 실행 순서: 테스트가 다른 테스트의 영향을 받는 경우 순서에 민감해질 수 있습니다. 테스트는 독립적으로 동작해야 하며, 이를 위해 격리된 환경에서 실행되도록 구성해야 합니다.
위 사항들을 바탕으로 현재 문제가 발생하는 테스트 코드와 설정을 살펴보시고, 적용해 보시는 것을 권장드립니다. 만약 여전히 문제가 지속된다면, 아래 유사한 문제를 다룬 질문을 참고하시면 도움이 될 수 있습니다.
- HelloRepositoryTest의 오류… - 이 게시물에서는 테스트에서의 빈 주입 문제와 관련된 해결책을 다루고 있습니다.
- 자동 구성 정보 분리가 안됩니다. - 클래스 로더를 통한 구성 정보 로딩 문제 해결 경험을 공유하고 있는 게시물입니다.
저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다. 현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏 추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.




