java - 为什么 junit 给我一个无法加载 ApplicationContext 错误

标签 java spring junit

我正在尝试在 Eclipse 中运行我的第一个 junit 类。当我右键单击新类并选择以 junit 身份运行时,它会出现无法加载应用程序上下文错误。 spring-servlet.xml 的直接路径是正确的。

代码如下:

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:C:/MyProject/WEB-INF/spring-servlet.xml" 
}) 
public class MyUnitTest {
@Autowired
private ApplicationContext applicationContext;

@Before
public void setUp() {
}

@Test
public void testFunctionality() throws Exception {
    assertTrue(true);
}
}

错误 -

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:scoped-proxy'.

最佳答案

The direct path to the spring-servlet.xml is correct.

不,不是。如果是的话,JUnit 就会找到它。

我认为它应该是这样的:

@ContextConfiguration(locations={"file:///C:/MyProject/WEB-INF/spring-servlet.xml"

http://en.wikipedia.org/wiki/File_URI_scheme

更新:

您发布的错误消息表明您确实读取了上下文 XML,但存在错误。

我发现将收到的任何错误消息粘贴到 Google 中会很有帮助。当我对你的进行此操作时,它向我发送了以下内容:

http://forum.springsource.org/showthread.php?35417-The-matching-wildcard-is-strict-but-no-declaration-can-be-foun...-aop-scoped-proxy

检查是否缺少 JAR。

关于java - 为什么 junit 给我一个无法加载 ApplicationContext 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6577015/

相关文章:

java - 在 java 中解析 log4j 模式

unit-testing - 如何将 JUnit 5 与 Gradle 结合使用?

java - IntelliJ IDE 中的 Junit 测试看不到用于测试的类

java - JUnit 询问用户测试是否成功?

java - 如何为这个特定任务选择和编写循环?

监控系统日期并自动发送电子邮件的Java程序

java - 如何在 BaseCommandController.bindAndValidate 中的某处调试 NumberFormatException

java - 如何在 Catalina.out 中禁用 hibernate 日志

java - 在 Java 中,为什么人们要在字段前面加上 `this` ?

java - 带有绑定(bind)变量的自定义 Spring 异常