简体   繁体   English

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

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

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 @ContextConfiguration I 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的新手,真的不知道这出错了。

The problem is that you use an absolute path in your @ContextConfiguration annotation. 问题是您在@ContextConfiguration批注中使用绝对路径。 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). 您现在指定applicationContext.xml位于系统的根文件夹中(可能不是,因为您说它位于/ etc / ws)。 I see two possible solutions: 我看到两种可能的解决方案

  1. Use @ContextConfiguration(locations={"/etc/ws/applicationContext.xml"}) 使用@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"}) 将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. 在第2种情况下,您必须确保将applicationContext.xml放在其中的目录位于类路径中。 In Eclipse this can be configured in your project properties. 在Eclipse中,可以在项目属性中配置它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Spring编写数据库单元测试:java.lang.IllegalStateException:无法加载ApplicationContext - Writing DB Unit Tests with Spring : java.lang.IllegalStateException: Failed to load ApplicationContext VS Code Java测试失败,“无法加载应用程序上下文” - VS Code Java tests failing with “Failed to load Application Context” 无法从测试中加载Spring应用程序上下文 - can not load Spring application context from tests 使用 Camel 的 Spring Boot 无法加载应用程序上下文 - Spring Boot with Camel failed to load application context 在单元测试中具有弹簧上下文 - having a spring context in unit tests 在Spring Web App中运行junit时,出现错误:java.lang.IllegalStateException:无法加载ApplicationContext - when running junit in spring web app, get an error: java.lang.IllegalStateException: Failed to load ApplicationContext 如何更正 Spring 引导单元测试无法在两个几乎相同的测试类之一中加载应用程序上下文? - How can I correct Spring Boot unit test failed to load application context in one of two nearly identical test classes? 单元测试spring服务类中的java.lang.NullPointerException - java.lang.NullPointerException in Unit Testing spring service class Spring-boot 应用。 在 JUnit 测试中加载 ApplicationContext 失败 - Spring-boot application. Failed to load ApplicationContext in JUnit tests Spring应用程序无法在Java 1.7中加载 - Spring application failed to load in Java 1.7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM