org.jboss.seam.mock
Class ResourceRequestEnvironment
java.lang.Object
org.jboss.seam.mock.ResourceRequestEnvironment
public class ResourceRequestEnvironment
- extends Object
Executes (through local calls, not TCP sockets) an HTTP request in a unit test, passing it through
the Seam resource handlers and filters.
This class is supposed to be used within a SeamTest, in fact, you need
to pass an instance of SeamTest into its constructor. This prepares the environment
for the resource request processing. You can either share an instance of the environment between
all your test methods (prepare it in @BeforeClass) or you can create a new instance
for each ResourceRequest:
import org.jboss.seam.mock.ResourceRequestEnvironment;
import org.jboss.seam.mock.EnhancedMockHttpServletRequest;
import org.jboss.seam.mock.EnhancedMockHttpServletResponse;
import static org.jboss.seam.mock.ResourceRequestEnvironment.ResourceRequest;
import static org.jboss.seam.mock.ResourceRequestEnvironment.Method;
public class MyTest extends SeamTest {
ResourceRequestEnvironment sharedEnvironment;
@BeforeClass
public void prepareSharedEnvironment() throws Exception {
sharedEnvironment = new ResourceRequestEnvironment(this) {
@Override
public Map getDefaultHeaders() {
return new HashMap() {{
put("Accept", "text/plain");
}};
}
};
}
@Test
public void test() throws Exception
{
//Not shared: new ResourceRequest(new ResourceRequestEnvironment(this), Method.GET, "/my/relative/uri)
new ResourceRequest(sharedEnvironment, Method.GET, "/my/relative/uri)
{
@Override
protected void prepareRequest(EnhancedMockHttpServletRequest request)
{
request.addQueryParameter("foo", "123");
request.addHeader("Accept-Language", "en_US, de");
}
@Override
protected void onResponse(EnhancedMockHttpServletResponse response)
{
assert response.getStatus() == 200;
assert response.getContentAsString().equals("foobar");
}
}.run();
}
}
Note that in a SeamTest the (mock) HTTP session is always shared between all requests in a particular test
method. Each test method however executes with a new (mock) HTTP session. Design your tests accordingly, this is not
configurable.
IMPORTANT: A ResourceRequest has to be executed in a @Test method or in a
@BeforeMethod callback. You can not execute it in any other callback, such * as @BeforeClass.
- Author:
- Christian Bauer
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
seamTest
protected final AbstractSeamTest seamTest
resourceServlet
protected final SeamResourceServlet resourceServlet
ResourceRequestEnvironment
public ResourceRequestEnvironment(AbstractSeamTest seamTest)
getServletPath
public String getServletPath()
getDefaultHeaders
public Map<String,Object> getDefaultHeaders()