junit4相对于junit3,基于注解的方式写单元测试用例,使用过程中方便很多。如下缩写均是代码片段,摘录其中关键部分,重要是理解其中知识点。
一、编写测试用例基类
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"file:src/main/webapp/WEB-INF/applicationContext.xml", "file:src/main/webapp/WEB-INF/spring-servlet.xml","file:src/main/webapp/WEB-INF/conf/spring-redis.xml", "file:src/main/webapp/WEB-INF/conf/spring-resttemplate.xml"})public abstract classBaseJunit
{/*** wac*/@AutowiredprivateWebApplicationContext wac;/*** MockMvc*/
privateMockMvc mockMvc;protectedWebApplicationContext getWac()
{return this.wac;
}protectedMockMvc getMockMvc()
{return this.mockMvc;
}/*** 初始化mocMvc
*
*@see
*/@Beforepublic voidsetUp()
{this.mockMvc = webAppContextSetup(this.wac).build();
}
...