Spring Web 服务单元测试:java.lang.IllegalStateExcepton:无法加载应用程序上下文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1831749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:09:35  来源:igfitidea点击:

Spring Web Service Unit Tests: java.lang.IllegalStateExcepton: Failed to load Application Context

javaspringjunit

提问by Xetius

I am getting the error "java.lang.IllegalStateExcepton: Failed to load Application Context" when I am trying to run my unit test from within Eclipse.

当我尝试从 Eclipse 中运行我的单元测试时,我收到错误“java.lang.IllegalStateExcepton:无法加载应用程序上下文”。

The Unit Test itself seems very simple:

单元测试本身看起来很简单:

package com.mycompany.interactive.cs.isales.ispr.ws.productupgrade;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
public class ProductUpgradeTest {

    @Test
    public void getProductUpgrade () throws Exception
    {
        //System.out.println(PrintClasspath.getClasspathAsString());
        Assert.assertTrue(true);
    }
}

But no matter what I seem to do with the @ContextConfigurationI still get the same error.

但是无论我似乎如何处理,@ContextConfiguration我仍然会遇到相同的错误。

The applicationContext.xml file resides within a folder /etc/ws, but even if I put a copy in the same folder it is still giving me errors.

applicationContext.xml 文件驻留在文件夹 /etc/ws 中,但即使我将副本放在同一个文件夹中,它仍然会给我错误。

I am very new to Java, Spring, Eclipse and Ant, and really don't know where this is going wrong.

我对 Java、Spring、Eclipse 和 Ant 很陌生,真的不知道这是哪里出了问题。

回答by Martin Sturm

The problem is that you use an absolute path in your @ContextConfigurationannotation. You now specify that your applicationContext.xml is located in your root folder of your system (where it probably isn't, since you say it is at /etc/ws). I see two possible solutions:

问题是您在@ContextConfiguration注释中使用了绝对路径。您现在指定 applicationContext.xml 位于系统的根文件夹中(它可能不在那里,因为您说它在 /etc/ws 中)。我看到两种可能的解决方案:

  1. Use @ContextConfiguration(locations={"/etc/ws/applicationContext.xml"})
  2. Move your applicationContext.xml file into your project (for example in your WEB-INF folder) and use @ContextConfiguration(locations={"classpath:applicationContext.xml"})
  1. 利用 @ContextConfiguration(locations={"/etc/ws/applicationContext.xml"})
  2. 将您的 applicationContext.xml 文件移动到您的项目中(例如在您的 WEB-INF 文件夹中)并使用 @ContextConfiguration(locations={"classpath:applicationContext.xml"})

In case 2, you have to make sure that the directory in which you place your applicationContext.xml is located in your classpath. In Eclipse this can be configured in your project properties.

在第 2 种情况下,您必须确保放置 applicationContext.xml 的目录位于类路径中。在 Eclipse 中,这可以在您的项目属性中进行配置。