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

标签 java spring spring-annotations applicationcontext

所以我有这个辅助类来获取应用程序上下文:

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

<?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>

当我运行测试时,出现此错误:

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
  • 我该如何解决这个问题? 我猜测这是由 spring core 版本错误引起的
  • 有更好的方法来测试 ApplicationContextProvider 吗?

谢谢!

最佳答案

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

尝试使用 3.2.4.RELEASE。

关于java - 测试 applicationContext : org. springframework.core.annotation.AnnotationUtils.getAnnotationAttributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25208024/

相关文章:

java - 在 Spring 中通过注释使用通配符将文件作为资源列表注入(inject)

c# - 使用pdfbox打开特定页面中的PDF文件

java - 如何根据数据库中的时间戳触发警报?

java - Hibernate 获取 CollectionOfElements Map 的值

Spring Security 不返回 UserDetails 对象,只返回用户名

java - 是否可以使用注释在方法退出时执行操作?

java - 发送海量数据——java解决方案

java - 如何在java中用遗传算法求解多项式方程?

spring - Java EE 与独立

java - 使用 Executor 和 WebSocket 的多线程