스프링 배치(Spring Batch) JUnit java.lang.IllegalStateException: Failed to load ApplicationContext 문제 해결 방법에 대해서 기록한다. 스프링 배치(Spring Batch)를 돌릴 때, 서버를 구동하지 않고 단위테스트인 JUnit으로 돌릴 경우에 발생했던 문제에 대해 기록한다.
문제 에러 전체
아래에서 봤을 때, RetryContextCache를 불러오지 못해 발생했던 에러다. 근데, 이 에러를 보고 구글링을 했었는데, 전부 다 @WebAppConfiguration Annotation을 추가하면 된다고 하는데, 이 문제가 아니었다.
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:93)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testCase00Step': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.configuration.xml.StepParserStepFactoryBean]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: org/springframework/retry/policy/RetryContextCache
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1159)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1103)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:928)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:805)
at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:573)
at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:601)
at org.springframework.batch.core.configuration.xml.CoreNamespacePostProcessor.injectJobRepositoryIntoSteps(CoreNamespacePostProcessor.java:71)
at org.springframework.batch.core.configuration.xml.CoreNamespacePostProcessor.postProcessBeanFactory(CoreNamespacePostProcessor.java:55)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:283)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:178)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:281)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:249)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 26 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.configuration.xml.StepParserStepFactoryBean]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: org/springframework/retry/policy/RetryContextCache
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1151)
... 43 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/retry/policy/RetryContextCache
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
... 44 more
Caused by: java.lang.ClassNotFoundException: org.springframework.retry.policy.RetryContextCache
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 49 more
문제 해결 방법
제일 먼저 에러를 봤을 때, @WebAppConfiguration Annotation을 넣어도 문제가 해결되지 않았다. 그래서, 이 문제를 해결하기 위해 확인해야 할 것을 아래에 적어둔다.
1. @WebAppConfiguration Annotation을 넣었는지 확인.
2. Java JDK 시스템 설정 환경변수 확인
3. JUnit 버전에 맞게 Build Path에 넣었는지.
4. pom.xml에 Junit 버전을 dependency로 등록을 했는지.
5. Junit jar 파일을 경로에 확실히 넣었는지.
6. Maven Update
여기까지 확인을 했는데도, 안되는 경우가 발생했다. 이 경우에 정말로, 혹시나 Maven 문제가 아닐까 해서 pom.xml을 봤는데, pom.xml, Effective POM, Overview, Dependencies를 봤는데 아무런 이상이 없었다.
근데, Dependency Hierarchy를 봤는데, 상단에 project read error 이 에러가 발생했다.
즉, 프로젝트에서 Maven을 제대로 읽지 못해 발생한 에러였다.
그래서, 이클립스 프로젝트의 워크스페이스 폴더 경로에 있는 로그를 확인했다.
workspace\.metadata\.plugins\ 이 경로에서 수정 시간이 제일 빠른 로그를 확인하면 된다.
아래와 같이, 로그가 찍힌다.
Caused by: org.eclipse.aether.transfer.ArtifactTransferException: com.fasterxml.jackson.core:jackson-annotations:pom:2.10.5 failed to transfer from https://maven.atlassian.com/3rdparty/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of oracle has elapsed or updates are forced. Original error: Could not transfer artifact com.fasterxml.jackson.core:jackson-annotations:pom:2.10.5 from/to oracle (https://maven.atlassian.com/3rdparty/): PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at org.eclipse.aether.internal.impl.DefaultUpdateCheckManager.newException(DefaultUpdateCheckManager.java:225)
at org.eclipse.aether.internal.impl.DefaultUpdateCheckManager.checkArtifact(DefaultUpdateCheckManager.java:189)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.gatherDownloads(DefaultArtifactResolver.java:573)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.performDownloads(DefaultArtifactResolver.java:483)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:401)
... 24 more
여기서 맨 밑에 에러를 보게 되면 어떤 jar에서 에러가 발생했는지를 확인할 수 있다.
그래서, 그 jar를 경로를 맞추던지, 버전을 맞추던지 하면 이 문제를 해결할 수 있다.
최근댓글