1.ContextConfiguration加载配置文件的时候,也要加载SpringMvc的配置文件
2.模拟请求的时候需要一个很重要的对象org.springframework.test.web.servlet.MockMvc MockMvc,该对象被初始化时需要方法 MockMvcBuilders.webAppContextSetup(context).build();来创建, 而context是Spring容器本身,获取方式为:在该测试类类名上添加注解@WebAppConfiguration,然后给WebApplicationContext context对象添加注解@AutoWired。
3.使用mockMvc对象进行模拟请求时,使用它的perform方法进行模拟
4.传一段代码,省的以后忘了
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@WebAppConfiguration
@ContextConfiguration(locations={"classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/springDispatcherServlet-servlet.xml"}) //加载配置文件
public class MvcTest {
//虚拟请求
MockMvc mockMvc;
//传入springMvc的IOC
@Autowired
WebApplicationContext context;
@Before
public void init() {
mockMvc = MockMvcBuilde