java 测试 applicationContext:org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25208024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:34:40  来源:igfitidea点击:

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

javaspringspring-annotationsapplicationcontext

提问by renz

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

使用以下资源文件(与测试类相同的包): 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.process(AnnotatedElementUtils.java:96)
    at org.springframework.core.annotation.AnnotatedElementUtils.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?
  • 我怎样才能解决这个问题?我猜这是由错误的 spring 核心版本引起的
  • 有什么更好的方法来测试ApplicationContextProvider吗?

Thanks!

谢谢!

采纳答案by renz

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

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

回答by 0xmilk_

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

我在测试 Spring Security 4.0.0.M1 时发现了同样的错误。使用 3.2.4.RELEASE 没有问题。

Try it with 3.2.4.RELEASE.

用 3.2.4.RELEASE 试试吧。

回答by Jose Leon

I have seen this error message when dealing with Apache Tomcat and webapps. A jar/class could be in the classpath when Apache Tomcat is being launched AND also be included within the webapp classes/lib folder.

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

The solution in this case is remove the duplicate class/jar.

这种情况下的解决方案是删除重复的类/jar。