I try to make unit test on Function queryInfo
of Class queryAction
:
public class queryAction{
public String queryInfo(){
// do something...
// this line throw Exception
HttpServletRequest request = ServletActionContext.getRequest();
String areaInfo = request.getParameter("paramJson");
// do something...
}
}
when the unit test is running, Reported the following error:
queryAction(com.huawei.provision.queryActionTest) Time elapsed: 0.047 sec <<< ERROR! java.lang.NullPointerException: null at org.apache.struts2.ServletActionContext.getRequest(ServletActionContext.java:112)
And I looked up some questions and answers, such as one way using Mockito and another way using easymock But I still don't know how to solve this problem by JMockit
.
I've taken the luxury of returning
areaInfo
inqueryInfo()
for this test.In your case, you should use
@Mocked
for both objects and return theHttpServletRequest
mock in the call fromServletActionContext.getRequest()
in an expectation.