1 package com.mockobjects.examples.dynamic; 2 3 import java.io.IOException ; 4 import java.io.PrintWriter ; 5 import java.io.StringWriter ; 6 7 import javax.servlet.ServletException ; 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpServletResponse ; 10 11 import junit.framework.TestCase; 12 13 import com.mockobjects.dynamic.C; 14 import com.mockobjects.dynamic.Mock; 15 import com.mockobjects.dynamic.OrderedMock; 16 17 22 23 public class SimpleServletTest extends TestCase { 24 final String SUBJECT = "Mail Subject"; 25 final String [] RECIPIENTS = new String []{ "tim@hotmail.com", "steve@yahoo.com"}; 26 final String BODY = "Mail Body"; 27 28 public SimpleServletTest(String name) { 29 super(name); 30 } 31 50 public void testDoGet() throws ServletException , IOException { 51 52 Mock mockHttpServletRequest = new Mock(HttpServletRequest .class); 53 Mock mockHttpServletResponse = new OrderedMock(HttpServletResponse .class, "Response with non-default name"); 54 Mock mockTimer = new Mock(Timer.class); 55 Mock mockMailSender = new Mock(MailSender.class); 56 57 mockHttpServletRequest.matchAndReturn( "getParameter", C.eq("browser-identifier"), "MSIE-5.0" ); 59 mockHttpServletRequest.matchAndReturn("getIntHeader", C.ANY_ARGS, 20); 60 61 mockHttpServletRequest.expectAndReturn( "getParameter", "subject", SUBJECT ); 62 mockHttpServletRequest.expectAndReturn("getParameterValues", "recipients", RECIPIENTS); 63 mockHttpServletRequest.expectAndReturn( "getParameter", C.args(C.eq("body")), BODY ); 64 mockTimer.expectAndReturn("getTime", 10000); 65 mockTimer.expectAndReturn("getTime", 20000); 66 mockMailSender.expectAndReturn("sendMail", C.eq(SUBJECT, RECIPIENTS, BODY),true); 67 68 final PrintWriter contentWriter = new PrintWriter (new StringWriter ()); 69 70 mockHttpServletResponse.expect( "setContentType", "text/html"); 71 mockHttpServletResponse.expectAndReturn( "getWriter", contentWriter ); 72 73 79 SimpleServlet aServlet = new SimpleServlet((Timer)mockTimer.proxy(), (MailSender)mockMailSender.proxy()); 80 aServlet.doGet((HttpServletRequest ) mockHttpServletRequest.proxy(), (HttpServletResponse ) mockHttpServletResponse.proxy()); 81 82 mockHttpServletRequest.verify(); 83 mockHttpServletResponse.verify(); 84 mockTimer.verify(); 85 mockMailSender.verify(); 86 } 87 } 88 | Popular Tags |