testing applicationContext: org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes

13,409

Solution 1

It turned out I should use spring-test-3.0.5, like the version of my other spring jars.

Solution 2

I found the same error while testing Spring Security 4.0.0.M1. With 3.2.4.RELEASE there is no trouble.

Try it with 3.2.4.RELEASE.

Share:
13,409
renz
Author by

renz

OCPJP(SE 6) - March 16,2013

Updated on June 17, 2022

Comments

  • renz
    renz almost 2 years

    So I have this helper class for getting the application context:

    public class ApplicationContextProvider implements ApplicationContextAware{
        private static ApplicationContext context;
        public static ApplicationContext getApplicationContext() {
            return context;
        }
        public void setApplicationContext(ApplicationContext context) throws BeansException {
            this.context = context;
        }
    }
    

    and its unit test:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration()
    public class ApplicationContextProviderTest {
    
        @Test
        public void getApplicationContextInstance(){
            assertNotNull(ApplicationContextProvider.getApplicationContext());
        }
    }
    

    with the following resource file (same package with test class): ApplicationContextProviderTest-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">
    
        <util:map id="contextProperties" />
    
        <bean id="applicationContextProvider"
              class="com.ideyatech.morphlynx.application.ApplicationContextProvider" />
    </beans>
    

    When I run the test, I get this error:

    java.lang.IllegalStateException: Failed to introspect annotations: class com.ideyatech.morphlynx.application.listener.ApplicationContextProviderTest
        at org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:169)
        at org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes(AnnotatedElementUtils.java:93)
        at org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes(AnnotatedElementUtils.java:87)
        at org.springframework.test.context.MetaAnnotationUtils$AnnotationDescriptor.<init>(MetaAnnotationUtils.java:305)
        at org.springframework.test.context.MetaAnnotationUtils$UntypedAnnotationDescriptor.<init>(MetaAnnotationUtils.java:367)
        at org.springframework.test.context.MetaAnnotationUtils$UntypedAnnotationDescriptor.<init>(MetaAnnotationUtils.java:362)
        at org.springframework.test.context.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:207)
        at org.springframework.test.context.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:180)
        at org.springframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration(ContextLoaderUtils.java:622)
        at org.springframework.test.context.DefaultTestContext.<init>(DefaultTestContext.java:93)
        at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:122)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:118)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:107)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
        at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
        at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
        at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:41)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    Caused by: java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes(Ljava/lang/annotation/Annotation;ZZ)Lorg/springframework/core/annotation/AnnotationAttributes;
        at org.springframework.core.annotation.AnnotatedElementUtils$4.process(AnnotatedElementUtils.java:96)
        at org.springframework.core.annotation.AnnotatedElementUtils$4.process(AnnotatedElementUtils.java:93)
        at org.springframework.core.annotation.AnnotatedElementUtils.doProcess(AnnotatedElementUtils.java:198)
        at org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:165)
        ... 30 more
    
    • How can I fix this? I'm guessing that this is caused by wrong versioning of spring core
    • Any better way to test ApplicationContextProvider?

    Thanks!