0

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

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

我该如何解决?

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

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

4

1 回答 1

1

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

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

或通过使用@BeforeClass 注释的方法对其进行初始化,因此服务器仅在此类中的所有测试之前启动一次,如下例所示:

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

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

于 2011-03-16T08:59:57.133 回答