1).commons-dbcp-1.4
2).junit-4.12
3).spring-test-3.1.4.RELEASE
4).org.hamcrest.core
5).spring框架其他相关依赖的jar包
注意:以上是在spring框架的基础上加入junit4, spring框架所需的jar包就不一一列出来了
列如:我们现在对一个登陆功能业务的接口测试
1).找到你要测试的接口或者接口实现类
2)单击右键---->new---->other---->junit---->junit test case---->next---->选择即将生成的测试类所在的package包(可选)---->next---->勾选要测试的方法(可选)---->finish
完成上面的步奏可以看到生成的测试类了
1).测试类应该继承 AbstractJUnit4SpringContextTests或者AbstractTransactionalJUnit4SpringContextTests
对于继承类的选择: 当测试类中需要用到事务管理时就用AbstractTransactionalJUnit4SpringContextTests
2).在测试类前面加上
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations"file:WebContent/WEB-INF/autolinkedadmin-servlet.xml")
引入spring配置文件
3).junit4 执行
右键方法名或者class 选择 Run As ----> junit test
结果: 断言预期值跟接口方法返回值不一致 测试失败:
结果: 断言预期值跟接口方法返回值一致 测试成功:
如何生成一个controller测试类在第二步已经介绍了,就不说了(也可以手动创建测试class)
1.首先在spring配置文件里面加入bean
<!-- junit4 测试Controller -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
2.new一个BaseControllerTest
public class BaseControllerTest {
private static HandlerMapping handlerMapping;
private static HandlerAdapter handlerAdapter;
@BeforeClass
public static void setUp() {
if (handlerMapping == null) {
String[] configs = {"file:WebContent/WEB-INF/autolinkedadmin-servlet.xml" };//这里配置文件根据实际项目相关
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocations(configs);
MockServletContext msc = new MockServletContext();
context.setServletContext(msc);
context.refresh();
msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,context);
handlerMapping = (HandlerMapping)
context.getBean(DefaultAnnotationHandlerMapping.class);
handlerAdapter = (HandlerAdapter)
context.getBean(context.getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);
}
}
public ModelAndView excuteController(HttpServletRequest request, HttpServletResponse response)throws Exception {
request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true);
HandlerExecutionChain chain = handlerMapping.getHandler(request);
final ModelAndView model = handlerAdapter.handle(request, response,
chain.getHandler());
return model;
}
}
3.controller测试类
一下代码包括了 1.正常的跳转页面 2.模拟页面传入的参数 3.模拟session
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "file:WebContent/WEB-INF/autolinkedadmin-servlet.xml")
public class UserControllerTest extends BaseControllerTest{
@Resource(name = "webSessionConstantMap")
protected Map<String, String> webSessionConstant; //模拟session时拿session的key(只在用到session时用,这里是根据spring注解拿的,实际情况跟项目走)
private static MockHttpServletRequest request = new MockHttpServletRequest();
private static MockHttpServletResponse response= new MockHttpServletResponse();
@Test
public void testShowAdminUserLogin() throws Exception{
request.setCharacterEncoding("UTF-8");
request.setRequestURI("/showAdminUserLogin");//要测试方法的url
request.setMethod(HttpMethod.POST.name());
//request.addParameter("login_userName", "admin123");
//request.addParameter("login_userPassword", "admin123"); //模拟页面传入的参数(只在controller需要接收页面参数时用到)
/*AdminUser user = new AdminUser();
user.setId(1L);
request.getSession().setAttribute(webSessionConstant.get("SESSION_USER"), user);*/ //模拟session(列如在跳转某些页面之前判断了是否登录,而登录的user对象放入了session中,就在这里模拟session的登录状态)
request.setMethod("POST");
ModelAndView mav = this.excuteController(request, response);
System.out.println(mav.getViewName());
assertEquals("forward:/WEB-INF/jsp/adminUser/login.jsp", mav.getViewName()); //比较一下是否是自己预期要跳转的页面
}
}