【问题标题】:Spring and testing with TestContextFramework使用 TestContext 框架进行 Spring 和测试
【发布时间】:2012-08-16 02:07:41
【问题描述】:

我想为使用 Spring MVC 的应用程序编写一些单元测试。我在 Spring 主页上阅读了手册页:http://static.springsource.org/spring/docs/current/spring-framework-reference/html/testing.html,看来这个 Spring 测试框架可能真的很有用。

但这里有一些问题:

1.如果我理解正确,要使用@ContextConfiguration之类的任何注释进行测试,我需要使用@RunWith(SpringJUnit4ClassRunner.class)吗?但是是否可以使用两个跑步者(可能不是),我只是想知道是否可以使用 Spring 跑步者和 mockito 一个,因为我正在使用 mockito 来模拟普通对象。

2.我有一个关于使用@ContextConfiguration 加载xml 上下文文件的问题。我在src/main/webapp/WEB-INF/spring 中有我的.xml 文件,如何使用ContextConfiguration 加载它们?我试过了

@ContextConfiguration(locations="/webapp/WEB-INF/spring/root-context.xml")
@ContextConfiguration(locations="webapp/WEB-INF/spring/root-context.xml")
@ContextConfiguration(locations="/WEB-INF/spring/root-context.xml")
@ContextConfiguration(locations="WEB-INF/spring/root-context.xml")
@ContextConfiguration(locations="/src/main/webapp/WEB-INF/spring/root-context.xml")
@ContextConfiguration(locations="src/main/webapp/WEB-INF/spring/root-context.xml")

但我总是得到class path resource [some_path] cannot be opened because it does not exist。我想知道我应该使用什么路径?

编辑: 第二个问题是 WEB-INF 不在类路径中,这里是帮助我的主题Location of spring-context.xml

【问题讨论】:

    标签: spring testing junit


    【解决方案1】:
    1. 不,您不能有两个 @RunWith,而是应该使用 Mockito 提供的其他方式来创建 Mock 替代。

    2. 指定多个位置的方式是这样的:

      @ContextConfiguration(locations={"first.xml", "second.xml"})
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      相关资源
      最近更新 更多