I have the following code.
HttpServletResponse response = mock(HttpServletResponse.class);
System.out.println("Response: " + response); // Prints "Mock for HttpServletResponse, hashCode: 2051435028"
ServletActionContext.setResponse(response);
System.out.println("Get Response: " + ServletActionContext.getResponse()); // Prints null.
ServletActionContext.getResponse())
prints null while response
is not null when setting.
How do I get the response object?
Mockito, Struts2, JUnit
Okay. I got a workaround. For others help...
I checked the code of getResponse()
public static HttpServletResponse getResponse() {
return (HttpServletResponse) ActionContext.getContext().get(HTTP_RESPONSE);
}
So I am already passing a mocked context
object to the ServletActionContext
. So before passing it, I am mocking the context call like this.
actionContext = mock(ActionContext.class);
when(actionContext.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE)).thenReturn(response);
Now it works!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.