一、变量等的初始化操作
@Before//每一个测试单元都会执行
@BeforeClass//只会执行一次,方法必须为静态方法
二、使用easymock测试
maven添加easymock依赖:
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.3.1</version>
</dependency>
//1.创建easymock,createMock只验证关联方法的调用次数不验证顺序
//createStrictMock调用次数和顺序都验证,同一个方法内的不同的createStrictMock对象的顺序分别验证,内部保持顺序一致即可
//IMocksControl可以验证一组对象的顺序(比如两个createStrictMock对象),而不仅仅是createStrictMock对象内部有序
IUserDao ud = EasyMock.createMock(IUserDao.class);
User u = new User("wcy","wcy","wcy");
//2.记录ud整个流程所有可能会发生的交互操作的结果
EasyMock.expect(ud.load("wcy")).andReturn(u);
//2.1没有返回值操作的记录
ud.delete("wcy");
//3.进入测试阶段
EasyMock.replay(ud);
//创建service与dao的关联
IUserService us = new UserService(ud);
us.delete("wcy");
User tu = us.load("wcy");
assertNotNull(tu);
assertEquals(tu.getUsername(), "wcy");
assertEquals(tu.getPassword(), "wcy");
assertEquals(tu.getNickname(), "wcy");
//4.验证交互过程是否正确
EasyMock.verify(ud);
三、基于容器的测试:
cactus:可以模拟J2EE的容器做测试
可以测试servlet,jsp,filter,ejb
添加maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
cactus测试注意点:不能走Spring MVC完整流程(不能走Servlet的过滤器链、SpringMVC的类型转换、数据验证、数据绑定、拦截器等等),如果做基本的测试没问题,这种方式就是纯粹的单元测试.
ModelMap mm = new ModelMap();
mm.addAttribute("user1", "wcy1");
ModelAndView mv = webController.welcomeName("hello", "wcy", mm);
//验证返回的ModelAndView是否正确
ModelAndViewAssert.assertModelAttributeValue(mv, "user1", "wcy1");
ModelAndViewAssert.assertViewName(mv, "index");
ModelAndViewAssert.assertModelAttributeAvailable(mv, "user");
四、一个完整的test.java
package com.wcy.controller;
import static org.junit.Assert.*;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.web.ModelAndViewAssert;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import com.wcy.dao.UserMsgDao;
import com.wcy.service.UserMsgService;
public class testServlet {
private ModelMap mm;
private UserMsgDao umd;
private UserMsgService wc;
private WebController webController;
@Before
public void setUp(){
webController = new WebController();
wc = new UserMsgService();
mm = EasyMock.createMock(ModelMap.class);
umd = EasyMock.createMock(UserMsgDao.class);
wc.setUserMsgDao(umd);
}
@Test
public void testWelcome() {
//录制mock对象将要执行的操作
EasyMock.expect(umd.insertQuery()).andReturn(1);
EasyMock.expect(mm.addAttribute("message", "Maven Web Project + Spring 3 MVC - welcome()")).andReturn(new ModelMap());
//调用这些操作
EasyMock.replay(umd,mm);
//测试一个函数,使其走之前预定的流程并且验证返回的结果
assertEquals("index", wc.welcome(mm));
EasyMock.verify(umd,mm);
}
@Test
public void testWelcomeName(){
MockHttpServletRequest req = new MockHttpServletRequest();
req.setAttribute("username", "hello world!");
req.setParameter("name", "hello china!");
ModelMap mm = new ModelMap();
mm.addAttribute("user1", "wcy1");
ModelAndView mv = webController.welcomeName("hello", "wcy", mm, req);
System.out.println(mv.getModel().get("message"));
ModelAndViewAssert.assertModelAttributeValue(mv, "user1", "wcy1");
ModelAndViewAssert.assertViewName(mv, "index");
ModelAndViewAssert.assertModelAttributeAvailable(mv, "user");
}
}