spring 版本 spring 3.2.0 的测试失败:无法加载 ApplicationContext

spring tests with version spring 3.2.0 fails: Failed to load ApplicationContext

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

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。 有人知道如何处理这个问题吗?

亲切的问候, Kladderradatsch

问题所在:java 版本错误。它可能会解决问题。在 Maven 配置中它说源和目标 1.8

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

将源和目标更改为 1.7

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