Spring并使用TestContextFramework进行测试

And*_*dna 0 testing junit spring

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

但这里有一些问题:

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

2.我有一个关于加载xml上下文文件的问题@ContextConfiguration.我有我的.xml文件src/main/webapp/WEB-INF/spring,如何使用它们加载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")
Run Code Online (Sandbox Code Playgroud)

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

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

Bij*_*men 5

  1. 不,你不能有两个@RunWith,相反你应该使用Mockito提供的其他方式来创建Mock替身.

  2. 指定多个位置的方法是:

    @ContextConfiguration(locations={"first.xml", "second.xml"})
    
    Run Code Online (Sandbox Code Playgroud)