【问题标题】:Spring MVC Testing Error in loading xml fie加载xml文件时的Spring MVC测试错误
【发布时间】:2011-03-16 05:10:44
【问题描述】:

我的代码有效,但在下面一行的 Junit 模拟测试中失败。

ApplicationContext ctx = new ClassPathXmlApplicationContext("../MyFile.xml");

我该如何解决这个问题?

添加@ContextConfiguration 还是有其他方法?

"ApplicationContext ctx = new ClassPathXmlApplicationContext("../MyFile.xml");"此行在普通方法中不在测试类中

【问题讨论】:

  • “fail”有点不具体——你说的“fail”是什么意思,例如,引发了哪个异常。

标签: java spring junit junit4 spring-test


【解决方案1】:

我不确定你的问题到底是什么,因为你已经提到了这两种解决方案,但当然你可以选择通过注释将它放在 testclass 上

@ContextConfiguration( locations = { "../applicationContext.xml" } )

或者通过使用@BeforeClass注解的方法来初始化它,所以服务器在这个类中的所有测试之前只启动一次,如下例所示:

@BeforeClass
public static void setUp()
{
  ApplicationContext ctx = new ClassPathXmlApplicationContext( "../applicationContext.xml" );
}

也许您需要解释初始化 ApplicationContext 时究竟是什么失败了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-26
    • 2021-06-13
    • 2016-11-09
    • 2016-05-16
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多