我正在尝试在 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 中会很有帮助。当我对你的进行此操作时,它向我发送了以下内容:
检查是否缺少 JAR。
关于java - 为什么 junit 给我一个无法加载 ApplicationContext 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6577015/