Java JUnit错误:";未能加载ApplicationContext";

Java JUnit错误:";未能加载ApplicationContext";,java,maven,spring-mvc,junit4,applicationcontext,Java,Maven,Spring Mvc,Junit4,Applicationcontext,似乎什么都不管用 我正在尝试运行简单测试: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/applicationContext.xml"}) @Transactional public class EmailServiceTest { 我也试过: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations

似乎什么都不管用

我正在尝试运行简单测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/applicationContext.xml"})
@Transactional
public class EmailServiceTest {
我也试过:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"src/main/webapp/WEB-INF/applicationContext.xml"})
@Transactional
public class EmailServiceTest {
还有一些不同的东西代替“位置”,比如“类路径”和“文件”

“ApplicationContext”位于:

src
    main
        webapp
            WEB-INF
                applicationContext.xml

但是JUnit测试仍然说:加载ApplicationContext失败

您需要在
@RunWith(SpringJUnit4ClassRunner.class)
之后使用
@ContextConfiguration(locations={“file:./src/main/resources/ApplicationContext.xml})。在
/src/test/java/your package name

创建test.java文件时,需要使用
@ContextConfiguration(locations={“file:./src/main/resources/ApplicationContext.xml})
@RunWith(SpringJUnit4ClassRunner.class)
之后。在
/src/test/java/your package name

创建test.java文件时,必须在类路径上指定应用程序上下文位置
src/main
被添加到类路径中。因此,您只需要指定
webapp/WEB-INF/applicationContext.xml
如下:
@ContextConfiguration(locations={“webapp/WEB-INF/applicationContext.xml”}

您必须在类路径上指定应用程序上下文位置
src/main
被添加到类路径中。因此,您只需要指定
webapp/WEB-INF/applicationContext.xml
如下:
@ContextConfiguration(locations={“webapp/WEB-INF/applicationContext.xml”}

在我们意识到构建文件缺少与测试相关的信息后,问题得到了解决。诸如“app.properties”和“applicationContext”之类的信息没有被复制到测试资源中。所以从技术上讲,这些都不在类路径上。

在我们意识到构建文件缺少与测试相关的信息后,问题得到了解决。诸如“app.properties”和“applicationContext”之类的信息没有被复制到测试资源中。因此,从技术上讲,这些都不在类路径上。

由于其他原因,可能会出现“加载应用程序上下文失败”的情况。检查stacktrace并找出此错误的原因。 在我的例子中,之所以出现此错误,是因为我在应用程序上下文xml文件名中输入了一个错误。

由于其他原因,可能会出现“加载应用程序上下文失败”的情况。检查stacktrace并找出此错误的原因。 在我的例子中,我得到这个错误是因为我在应用程序上下文xml文件名中有一个输入错误。

否,它会这样说,并提供一个详细的错误消息,比如说它正在尝试查找哪个bean,但找不到。发那个消息。在这种情况下,可能是因为您的
applicationContext.xml
不在类路径上的任何位置。如果您是Spring新手,我强烈建议您使用Spring引导并遵循其约定(包括不使用遗留XML),直到您对Spring有了更全面的了解。不,它会这样说,并提供详细的错误消息,比如说它试图查找哪个bean,但找不到。发那个消息。在这种情况下,可能是因为您的
applicationContext.xml
不在类路径上的任何位置。如果您是Spring新手,我强烈建议您使用Spring引导并遵循其约定(包括不使用遗留XML),直到您对Spring有了更多的了解。