|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.stripes.mock.MockRoundtrip
public class MockRoundtrip
Mock object that attempts to make it easier to use the other Mock objects in this package to interact with Stripes and to interrogate the results. Everything that is done in this class is do-able without this class! It simply exists to make things a bit easier. As a result all the methods in this class simply manipulate one or more of the underlying Mock objects. If some needed capability is not exposed through the MockRoundtrip it is always possible to fetch the underlying request, response and context and interact with them directly.
It is worth noting that the Mock system does not process forwards, includes and redirects. When an ActionBean (or other object) invokes the servlet APIs for any of these actions it is recorded so that it can be reported and verified later. In the majority of cases it should be sufficient to test ActionBeans in isolation and verify that they produced the expected output data and/or forward/redirect. If your ActionBeans depend on being able to include other resources before continuing, sorry - you're on your own!
An example usage of this class might look like:
MockServletContext context = ...; MockRoundtrip trip = new MockRoundtrip(context, CalculatorActionBean.class); trip.setParameter("numberOne", "2"); trip.setParameter("numberTwo", "2"); trip.execute(); CalculatorActionBean bean = trip.getActionBean(CalculatorActionBean.class); Assert.assertEquals(bean.getResult(), 4, "two plus two should equal four"); Assert.assertEquals(trip.getDestination(), ""/quickstart/index.jsp");
Field Summary | |
---|---|
static String |
DEFAULT_SOURCE_PAGE
Default value for the source page that generated this round trip request. |
Constructor Summary | |
---|---|
MockRoundtrip(MockServletContext context,
Class<? extends ActionBean> beanType)
Preferred constructor that will manufacture a request. |
|
MockRoundtrip(MockServletContext context,
Class<? extends ActionBean> beanType,
MockHttpSession session)
Preferred constructor that will manufacture a request. |
|
MockRoundtrip(MockServletContext context,
String actionBeanUrl)
Constructor that will create a requeset suitable for the provided servlet context and URL. |
|
MockRoundtrip(MockServletContext context,
String actionBeanUrl,
MockHttpSession session)
Constructor that will create a requeset suitable for the provided servlet context and URL. |
Method Summary | ||
---|---|---|
void |
addParameter(String name,
String... value)
Adds the value provided to the set of values for the named request parameter. |
|
void |
execute()
Executes the request in the servlet context that was provided in the constructor. |
|
void |
execute(String event)
Executes the request in the servlet context that was provided in the constructor. |
|
|
getActionBean(Class<A> type)
Gets the instance of the ActionBean type provided that was instantiated by Stripes to handle the request. |
|
MockServletContext |
getContext()
Get the ActionBean context to be used by this round trip |
|
String |
getDestination()
Gets the URL to which Stripes was directed after invoking the ActionBean. |
|
String |
getForwardUrl()
If the request resulted in a forward, returns the URL that was forwarded to. |
|
byte[] |
getOutputBytes()
Gets, as bytes, any data that was written to the output stream associated with the request. |
|
String |
getOutputString()
Gets, as a String, any data that was written to the output stream associated with the request. |
|
String |
getRedirectUrl()
If the request resulted in a redirect, returns the URL that was redirected to. |
|
MockHttpServletRequest |
getRequest()
Get the servlet request object to be used by this round trip |
|
MockHttpServletResponse |
getResponse()
Get the servlet response object to be used by this round trip |
|
ValidationErrors |
getValidationErrors()
Gets the (potentially empty) set of Validation Errors that were produced by the request. |
|
protected void |
setContext(MockServletContext context)
Set the ActionBean context to be used by this round trip |
|
void |
setParameter(String name,
String... value)
Sets the named request parameter to the value or values provided. |
|
protected void |
setRequest(MockHttpServletRequest request)
Set the servlet request object to be used by this round trip |
|
protected void |
setResponse(MockHttpServletResponse response)
Set the servlet response object to be used by this round trip |
|
void |
setSourcePage(String url)
All requests to Stripes that can generate validation errors are required to supply a request parameter telling Stripes where the request came from. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String DEFAULT_SOURCE_PAGE
Constructor Detail |
---|
public MockRoundtrip(MockServletContext context, Class<? extends ActionBean> beanType)
context
- the MockServletContext that will receive this requestbeanType
- a Class object representing the ActionBean that should receive the requestpublic MockRoundtrip(MockServletContext context, Class<? extends ActionBean> beanType, MockHttpSession session)
context
- the MockServletContext that will receive this requestbeanType
- a Class object representing the ActionBean that should receive the requestpublic MockRoundtrip(MockServletContext context, String actionBeanUrl)
context
- the MockServletContext that will receive this requestactionBeanUrl
- the url binding of the action beanpublic MockRoundtrip(MockServletContext context, String actionBeanUrl, MockHttpSession session)
context
- the MockServletContext that will receive this requestactionBeanUrl
- the url binding of the action beansession
- an instance of MockHttpSession to use for the requestMethod Detail |
---|
public MockHttpServletRequest getRequest()
protected void setRequest(MockHttpServletRequest request)
public MockHttpServletResponse getResponse()
protected void setResponse(MockHttpServletResponse response)
public MockServletContext getContext()
protected void setContext(MockServletContext context)
public void setParameter(String name, String... value)
public void addParameter(String name, String... value)
public void setSourcePage(String url)
public void execute() throws Exception
Exception
public void execute(String event) throws Exception
Exception
public <A extends ActionBean> A getActionBean(Class<A> type)
type
- the Class object representing the ActionBean type expected
public ValidationErrors getValidationErrors()
public byte[] getOutputBytes()
public String getOutputString()
public String getDestination()
public String getForwardUrl()
public String getRedirectUrl()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |