Created
March 20, 2012 12:20
-
-
Save froop/2134584 to your computer and use it in GitHub Desktop.
[Java][Servlet] 単体テスト用HttpSessionスタブ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.servlet.http.HttpSession; | |
public class HttpSessionStub implements HttpSession { | |
private final Map<String, Object> attributes = new HashMap<String, Object>(); | |
@Override | |
public Object getAttribute(String name) { | |
return attributes.get(name); | |
} | |
@Override | |
public void setAttribute(String name, Object value) { | |
attributes.put(name, value); | |
} | |
@Override | |
public void removeAttribute(String name) { | |
attributes.remove(name); | |
} | |
@Override | |
public Enumeration<String> getAttributeNames() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public long getCreationTime() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public String getId() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public long getLastAccessedTime() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public int getMaxInactiveInterval() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public ServletContext getServletContext() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public HttpSessionContext getSessionContext() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public Object getValue(String name) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public String[] getValueNames() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void invalidate() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public boolean isNew() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void putValue(String name, Object value) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void removeValue(String name) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void setMaxInactiveInterval(int arg0) { | |
throw new UnsupportedOperationException(); | |
} | |
@Test | |
public void testAttribute() { | |
HttpSession session = new HttpSessionStub(); | |
session.setAttribute("key1", "value1"); | |
assertEquals("value1", session.getAttribute("key1")); | |
session.removeAttribute("key1"); | |
assertNull(session.getAttribute("key1")); | |
} | |
@Test | |
public void testAttributeNull() { | |
HttpSession session = new HttpSessionStub(); | |
assertNull(session.getAttribute("key1")); | |
session.removeAttribute("key1"); | |
assertNull(session.getAttribute("key1")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment