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

时间:2016-01-26 13:05:47

标签: spring maven spring-test

我在Eclipse中的基于Maven的项目正在尝试测试一个内部有一个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

1 个答案:

答案 0 :(得分:3)

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

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

将来源和目标更改为1.7

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