其他分享
首页 > 其他分享> > 使用spring 3.2.0版本的spring测试失败:无法加载ApplicationContext

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

作者:互联网

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

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

解决方法:

这就是问题:错误的Java版本.它可能会解决问题.在Maven配置中它说源和目标1.8

因此,您应该使用Java 7或升级到Spring 4.

将源和目标更改为1.7

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

标签:maven,spring-test,spring
来源: https://codeday.me/bug/20191119/2033685.html