0

I wanted to write few unit tests for my application that uses Spring MVC. I read the manual page at the Spring home page:http://static.springsource.org/spring/docs/current/spring-framework-reference/html/testing.html and it seems that this Spring testing framework could be really useful.

But here are the questions:

1.If I understand it right, to use any of the annotations for testing like @ContextConfiguration I need to use @RunWith(SpringJUnit4ClassRunner.class)? But is it possible to use two runners (probably not), I just wondered if one can use Spring runner and mockito one, because I am using mockito to mock normal objects.

2.I have a question about loading xml context files with @ContextConfiguration. I have my .xml files in src/main/webapp/WEB-INF/spring, how can I load them using ContextConfiguration? I tried

@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")

but I always get class path resource [some_path] cannot be opened because it does not exist.I am wondering then what path should I use?

EDIT: the second problem was that WEB-INF is not in the classpath, here is the topic that helped me Location of spring-context.xml

4

1 回答 1

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

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

    @ContextConfiguration(locations={"first.xml", "second.xml"})
    
于 2012-08-20T19:20:44.563 回答