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