【问题标题】:spring tests with version spring 3.2.0 fails: Failed to load ApplicationContext使用 spring 3.2.0 版本的 spring 测试失败:无法加载 ApplicationContext
【发布时间】:2016-05-03 01:21:15
【问题描述】:

我在 Eclipse 中基于 Maven 的项目正在尝试测试一个包含一个 bean 的简单 Spring 容器,但测试总是失败:

java.lang.IllegalStateException: Failed to load ApplicationContext
            at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
            at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    ...
    Caused by: java.lang.IllegalArgumentException
        at org.springframework.asm.ClassReader.<init>(Unknown Source)

在这里你可以看到我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company.test</groupId>
    <artifactId>springtest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <spring_version>3.2.0.RELEASE</spring_version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring_version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring_version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

它遵循我的 Spring 应用程序上下文(非常简单):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="customer" class="com.company.test.beans.Customer">
    <property name="name" value="Axel Acker" />
    <property name="id" value="01" />
  </bean> 
</beans>

最后是不起作用的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class TestCustomer {

    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void testXAutoApplicationContext() throws Exception {
        Assert.assertNotNull(applicationContext);
    }

    @Test
    public void test() {
        Customer customer = (Customer) applicationContext.getBean("customer");
        Assert.assertNotNull(customer);
        System.out.println(customer.getName());
    }
}

是的,上下文文件放置在正确的站点上:src/resources/applicationContext.xml

eclipse project

使用 Spring 4.2.0,所有测试都很好,但我必须继续使用 Spring 3.2.0。 有人知道如何处理这个问题吗?

亲切的问候, 克拉德拉达奇

【问题讨论】:

  • 能不能通过github分享代码
  • 很遗憾没有。我没有 GitHub 帐户。
  • 你在用 Java 8 运行这个吗?
  • 是的,Java 8 正在使用中。
  • 所以尝试更新到最新的 Spring 3.2.x 版本,他们从 3.2.9 开始集成了对 Java 8 的一些支持

标签: spring maven spring-test


【解决方案1】:

这就是问题所在:错误的 java 版本。它可能会解决问题。在 Maven 配置中,它显示源和目标 1.8

所以你应该使用 java 7 或升级到 Spring 4。

将源和目标更改为 1.7

感谢@kladderradatsch 更新 Spring 后应使用更新版本的 Java JDK。

【讨论】:

  • 你完全正确!这就是解决方案!非常感谢!
  • 升级到spring 3.2.16.RELEASE后,现在可以使用JDK 8了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多