/**数据库测试往往会破坏数据库原有的数据,所以需要stub(桩)测试, * 将测试放置到一个不相干单独的空间进行 * 例子:1.一般数据库测试之前需要将数据库原有数据备份 * 2.等数据库测试完成操作之后再将数据库原有数据恢复 * 3.DBunit就是这种工作方式 * 《stub aren't mock》 * stub强调状态,mock强调交互 * sourceforget.net下载jar*/ public class UserStubDao implements IUserDao { private Map<String,User> us=new HashMap<String,User>(); @Override public void add(User user) { us.put(user.getUsername(),user); } @Override public void delete(String username) { us.remove(username); } @Override public User load(String username) { return us.get(username); } } /////////////////////// public class TestJettyInCactus { public static Test suite(){ System.setProperty("cactus.contextURL", "http://localhost:2323/junit"); TestSuite suite=new TestSuite(); suite.addTestSuite(TestLoginServletByCactus.class); return new Jetty5xTestSetup(suite); } } ////////////////////////// /**catcus:catcus主要是基于junit3.8进行操作,并不支持junit4的annotation 1.导入jar包 2.创建servlet的测试类 只要写一个类继承ServletTestCase,就会拥有相应的request方法 需要注意一点:此时junit4中的annotation全部不起作用 就得按照junit3的方式进行:setUp和tearDown,testXXX测试*/ public class TestLoginServletByCactus extends ServletTestCase { private LoginServlet servlet; public void testSession(){ session.setAttribute("loginUser",new User()); } /**begin是在客户端执行的*/ public void beginNoSession(WebRequest request){ request.setAutomaticSession(false); request.addParameter("username","kh"); } /**setUp子服务端执行*/ public void setUp(){ servlet=new LoginServlet(); } /**服务器端执行*/ public void testNoSession(){ //session.setAttribute("loginUser",new User()); Assert.assertFalse(servlet.isLogin(request)); Assert.assertEquals(request.getParameter("username"),"kh"); } public void testSessionNoUser(){ Assert.assertFalse(servlet.isLogin(request)); } public void testSessionHasUser(){ session.setAttribute("loginUser",new User()); Assert.assertTrue(servlet.isLogin(request)); } public void testDoGet() throws ServletException, IOException{ servlet.doGet(request,response); } public void endDoGet(WebResponse reponse){ try { System.out.println(reponse.getTables()[0].getCellAsText(0,0)); Assert.assertEquals(reponse.getTables()[0].getCellAsText(0,0), "111"); Assert.assertEquals(reponse.getTables()[0].getCellAsText(0,1), "222"); } catch (SAXException e) { e.printStackTrace(); } } } ////////////////////////////////////// public class TestLoginServletByMock { private LoginServlet servlet; private HttpServletRequest request; private HttpSession session; @Before public void setUp(){ servlet=new LoginServlet(); request=createStrictMock(HttpServletRequest.class); session=createStrictMock(HttpSession.class); } @Test public void testSessionIsNull(){ expect(request.getSession()).andReturn(null); replay(request,session); Assert.assertFalse(servlet.isLogin(request)); verify(request,session); } @Test public void testSessionNoUser(){ expect(request.getSession()).andReturn(session); expect(session.getAttribute("loginUser")).andReturn(null); replay(request,session); Assert.assertFalse(servlet.isLogin(request)); verify(request,session); } @Test public void testSessionHasUser(){ expect(request.getSession()).andReturn(session); expect(session.getAttribute("loginUser")).andReturn(new User()); replay(request,session); Assert.assertTrue(servlet.isLogin(request)); verify(request,session); } } //////////////////////////////////// catcus.properties: cactus.contextURL=http://localhost:8080/Java96_junit/