Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring框架:未能加载ApplicationContext_Spring - Fatal编程技术网

Spring框架:未能加载ApplicationContext

Spring框架:未能加载ApplicationContext,spring,Spring,在Spring框架中运行脚本时,它会抛出错误消息“加载ApplicationContext失败”。我的ApplicationContext.xml文件位于src/test/resources下 我用的是 @RunWith(value = Parameterized.class) public class CustomerTest extends FunctionLibrary { ... } 经过论坛的一些建议,我使用了 @RunWith(value = Parameterized.class

在Spring框架中运行脚本时,它会抛出错误消息“加载ApplicationContext失败”。我的ApplicationContext.xml文件位于src/test/resources下 我用的是

@RunWith(value = Parameterized.class)
public class CustomerTest extends FunctionLibrary { ... }
经过论坛的一些建议,我使用了

@RunWith(value = Parameterized.class)
@ContextConfiguration(locations = "file:src/test/resources/applicationContext.xml")
public class CustomerTest extends FunctionLibrary { ... }
我仍然收到相同的错误消息。请告诉我如何解决这个问题

谢谢


苏丹苏有几个问题

1) 使用
@RunWith(SpringJUnit4ClassRunner.class)

2) 您的路径听起来像是有一个maven项目。在maven项目中,当测试运行时,
src/test/resources
将被添加到类路径中。因此,请使用:

@ContextConfiguration(locations = "classpath:applicationContext.xml")
如果您在
src/main/resources
中还有其他同名文件,那么我建议将
src/test/resources
中的文件重命名为
test applicationContext.xml
,然后使用
@ContextConfiguration(locations=“classpath:test applicationContext.xml”)
,只是为了减少你大脑中的结数

完整示例:

package com.test.service;

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(classpath:test-ApplicationContext.xml)
public class WhateverTest {
...
}
pom摘录:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>3.2.6.RELEASE</version>
 </dependency>

朱尼特
朱尼特
4.11
测试
org.springframework
弹簧试验
3.2.6.1发布

您通常通过首先读取异常堆栈跟踪来解决问题。对于初学者,您需要删除@RunWith(Parameterized.class)并将其替换为@RunWith(SpringJUnit4ClassRunner.class)(这意味着您的JUnit测试将不会参数化,您需要找到其他方法来实现参数化功能)将@RunWith(SpringJUnit4ClassRunner.class)替换为@RunWith(Parameterized.class)…之后,会显示错误消息,如类无法解析为类型..如何重新读取此错误消息?类路径上是否存在spring测试依赖项?是。。。org.springframework spring test${org.springframework version}我在@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations=“classpath:applicationContext.xml”)中添加了这两个注释但是它显示了Junit初始化错误…如何修复初始化错误..我认为无法找到applicationContext.xml的路径file@user3411418:您需要发布初始化错误-我猜不出它们。java.lang.Exception:测试类至少应该有一个公共零参数构造函数org.junit.runners.BlockJUnit4ClassRunner.validatezerogconstructor(BlockJUnit4ClassRunner.java:147)在org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:124)在org.junit.runners.BlockJUnit4ClassRunner.collectionInitializationErrors(BlockJUnit4ClassRunner.java:103)@user3411418:真的吗?开始读取异常!“测试类应该正好有一个公共零参数构造函数”我有一个参数化构造函数公共CustomerTest(配置)抛出异常{super(配置);/}