JUnit Error: "Failed to load ApplicationContext"
JUnit Error: "Failed to load ApplicationContext"
似乎没有任何效果
我正在尝试 运行 简单测试:
@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 {
还有一些不同的东西代替 "location,",例如 "classpath" 和 "file."
"ApplicationContext" 位于:
src
main
webapp
WEB-INF
applicationContext.xml
但 JUnit 测试仍然提示:加载 ApplicationContext 失败
您需要在 @RunWith(SpringJUnit4ClassRunner.class)
之后使用 @ContextConfiguration(locations = { "file:./src/main/resources/ApplicationContext.xml" })
。 test.java 文件创建于 /src/test/java/your-package-name
.
您必须在类路径上指定应用程序上下文位置。 src/main
添加到类路径中。因此,您只需指定 webapp/WEB-INF/applicationContext.xml
,如下所示:@ContextConfiguration(locations = {"webapp/WEB-INF/applicationContext.xml"})
.
在我们意识到我们的构建文件缺少与测试有关的信息后,问题得到了解决。 "app.properties" 和“applicationContext”等信息未被复制到测试资源中。所以从技术上讲,其中 none 个在类路径上。
“无法加载 ApplicationContext”可能由于其他原因而发生。浏览堆栈跟踪并找到此错误的原因。
就我而言,我收到此错误是因为我在应用程序上下文 xml 文件名中输入错误。
似乎没有任何效果
我正在尝试 运行 简单测试:
@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 {
还有一些不同的东西代替 "location,",例如 "classpath" 和 "file."
"ApplicationContext" 位于:
src
main
webapp
WEB-INF
applicationContext.xml
但 JUnit 测试仍然提示:加载 ApplicationContext 失败
您需要在 @RunWith(SpringJUnit4ClassRunner.class)
之后使用 @ContextConfiguration(locations = { "file:./src/main/resources/ApplicationContext.xml" })
。 test.java 文件创建于 /src/test/java/your-package-name
.
您必须在类路径上指定应用程序上下文位置。 src/main
添加到类路径中。因此,您只需指定 webapp/WEB-INF/applicationContext.xml
,如下所示:@ContextConfiguration(locations = {"webapp/WEB-INF/applicationContext.xml"})
.
在我们意识到我们的构建文件缺少与测试有关的信息后,问题得到了解决。 "app.properties" 和“applicationContext”等信息未被复制到测试资源中。所以从技术上讲,其中 none 个在类路径上。
“无法加载 ApplicationContext”可能由于其他原因而发生。浏览堆栈跟踪并找到此错误的原因。 就我而言,我收到此错误是因为我在应用程序上下文 xml 文件名中输入错误。