Java 测试应用程序上下文:org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes

Java 测试应用程序上下文:org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes,java,spring,spring-annotations,applicationcontext,Java,Spring,Spring Annotations,Applicationcontext,因此,我有一个助手类用于获取应用程序上下文: public class ApplicationContextProvider implements ApplicationContextAware{ private static ApplicationContext context; public static ApplicationContext getApplicationContext() { return context; } public

因此,我有一个助手类用于获取应用程序上下文:

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;
    }
}
及其单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
public class ApplicationContextProviderTest {

    @Test
    public void getApplicationContextInstance(){
        assertNotNull(ApplicationContextProvider.getApplicationContext());
    }
}
使用以下资源文件(与测试类相同的包): ApplicationContextProviderTest-context.xml


运行测试时,出现以下错误:

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
java.lang.IllegalStateException:未能内省批注:class com.ideyatech.morphlynx.application.listener.ApplicationContextProviderTest
位于org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:169)
位于org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes(AnnotatedElementUtils.java:93)
位于org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes(AnnotatedElementUtils.java:87)
位于org.springframework.test.context.MetaAnnotationUtils$AnnotationDescriptor。(MetaAnnotationUtils.java:305)
位于org.springframework.test.context.MetaAnnotationUtils$UntypedAnnotationDescriptor。(MetaAnnotationUtils.java:367)
位于org.springframework.test.context.MetaAnnotationUtils$UntypedAnnotationDescriptor。(MetaAnnotationUtils.java:362)
位于org.springframework.test.context.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:207)
位于org.springframework.test.context.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:180)
位于org.springframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration(ContextLoaderUtils.java:622)
位于org.springframework.test.context.DefaultTestContext。(DefaultTestContext.java:93)
位于org.springframework.test.context.TestContextManager。(TestContextManager.java:122)
位于org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:118)
位于org.springframework.test.context.junit4.SpringJUnit4ClassRunner。(SpringJUnit4ClassRunner.java:107)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
位于java.lang.reflect.Constructor.newInstance(Constructor.java:526)
位于org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
位于org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
位于org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
位于org.junit.internal.builders.AllDefaultPossibilityBuilder.runnerForClass(AllDefaultPossibilityBuilder.java:29)
位于org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
位于org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
位于com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:41)
位于com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
位于com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
原因:java.lang.NoSuchMethodError:org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes(Ljava/lang/annotation/annotation;ZZ)Lorg/springframework/core/annotation/AnnotationAttributes;
位于org.springframework.core.annotation.AnnotatedElementUtils$4.process(AnnotatedElementUtils.java:96)
位于org.springframework.core.annotation.AnnotatedElementUtils$4.process(AnnotatedElementUtils.java:93)
位于org.springframework.core.annotation.AnnotatedElementUtils.doProcess(AnnotatedElementUtils.java:198)
位于org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:165)
... 30多
  • 我怎样才能解决这个问题?我猜这是由SpringCore的错误版本控制引起的
  • 有没有更好的方法来测试ApplicationContextProvider

谢谢

我在测试Spring Security 4.0.0.M1时发现了相同的错误。 对于3.2.4.1版本,没有问题


用3.2.4.RELEASE试试。

结果证明我应该使用spring-test-3.0.5,就像我的其他spring-jar版本一样

我在处理ApacheTomcat和webapps时看到了这个错误消息。 在启动ApacheTomcat时,jar/类可以位于类路径中,也可以包含在webapp classes/lib文件夹中


本例中的解决方案是删除重复的类/jar。

我猜这是由spring core的错误版本控制引起的。向我们展示您正在使用的版本。该项目使用core-3.0.5.RELEASE,但随后我收到一个错误“由以下原因引起:java.lang.ClassNotFoundException:org.springframework.core.annotation.AnnotatedElementUtils”。我在类路径中添加了core-4.0.5.RELEASE,那是我在帖子中遇到错误的时候。其他Spring libs呢?大多数也是3.0.5.RELEASE或3.X.X.RELEASE不要混合使用。让他们都一样。
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