Java Reference
In-Depth Information
Listing 8.2
Testing a servlet with EasyMock
[...]
import javax.servlet.http.HttpServletRequest;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.easymock.EasyMock.eq;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
[...]
public class EasyMockSampleServletTest {
B
private SampleServlet servlet;
private HttpServletRequest mockHttpServletRequest;
private HttpSession mockHttpSession;
C
@Before
public void setUp() {
servlet = new SampleServlet();
mockHttpServletRequest =
createStrictMock(HttpServletRequest. class );
mockHttpSession = createStrictMock(HttpSession. class );
}
D
@Test
public void testIsAuthenticatedAuthenticated() {
expect(mockHttpServletRequest.getSession(eq( false )))
.andReturn(mockHttpSession);
expect(mockHttpSession.getAttribute(eq("authenticated")))
.andReturn("true");
replay(mockHttpServletRequest);
replay(mockHttpSession);
assertTrue(servlet.isAuthenticated(mockHttpServletRequest));
}
E
F
G
@Test
public void testIsAuthenticatedNotAuthenticated() {
expect(mockHttpSession.getAttribute(eq("authenticated")))
.andReturn("false");
replay(mockHttpSession);
expect(mockHttpServletRequest.getSession(eq(false)))
.andReturn(mockHttpSession);
replay(mockHttpServletRequest);
assertFalse(servlet.isAuthenticated(mockHttpServletRequest));
}
@Test
public void testIsAuthenticatedNoSession() {
expect(mockHttpServletRequest.getSession(eq( false ))).andReturn( null );
replay(mockHttpServletRequest);
replay(mockHttpSession);
assertFalse(servlet.isAuthenticated(mockHttpServletRequest));
}
 
Search WWH ::




Custom Search