我在模拟 requestURI() 时遇到问题。在这个问题上的任何帮助都将是有益的,我正在尝试使用下面的 mockito 来模拟
String reqURI25 = req.getRequestURI().substring(req.getContextPath().length());
我试过了
Mockito.when(httpServletRequest.getRequestURI().substring(httpServletRequest.getContextPath().length())).thenReturn("/test/test");
Mockito.when(httpServletRequest.getParameter("name")).thenReturn(name);
Mockito.when(httpServletRequest.getParameter("category")).thenReturn(category);
异常 - 我得到 java.lang.NullPointerException
String reqURI25 = req.getRequestURI().substring(req.getContextPath().length());
private String category="test";
private String name="test";
private String isoLanguage="en";
private String isoCountry="US";
private String countryName="United States of America";
private String isoLCCountry="us";
private String charsetEncoding="iso-8859-1";
String reqURI25 = req.getRequestURI().substring(req.getContextPath().length());
//determine the last forward slash
//to the right is the form name
//to the left is the category
int lastSlash = reqURI25.lastIndexOf("/");
int uriLen = reqURI25.length();
String form_name="";
String init_form_name = reqURI25.substring(lastSlash + 1);
boolean skipLastChar = false;
if (((lastSlash + 9) == uriLen) && (init_form_name.equals("director")))
{
Enumeration parameters = req.getParameterNames();
while (parameters.hasMoreElements()) {
String parameterName = (String)parameters.nextElement();
if (parameterName.equals("form_name")) {
form_name = req.getParameter(parameterName);
}
}
if (!form_name.equals("")) {
reqURI25 = reqURI25.substring(0, lastSlash) + "/" + form_name;
lastSlash = reqURI25.lastIndexOf("/");
uriLen = reqURI25.length();
logger.debug(method,"reconfigured formname in querystring reqURI25 = "+reqURI25);
}
}
if ((lastSlash + 1) == uriLen)
{
lastSlash = reqURI25.substring(0, uriLen-1).lastIndexOf("/");
skipLastChar = true;
}
logger.debug(method,"lastSlash = "+lastSlash);
//category=req.getParameter("category"); // old 2.0 engine
// handle for if lastSlash = 0, there is no category
if (lastSlash == 0)
{
category="";
}
else
{
category=reqURI25.substring(1, lastSlash);
}
logger.debug(method,"category = "+category);
if (skipLastChar)
{
name=reqURI25.substring(lastSlash + 1, uriLen - 1);
}
else
{
name=reqURI25.substring(lastSlash + 1);
}
logger.debug(method,"name = "+name);
if (name.equals("form.do"))
{
name=req.getParameter("name");
category=req.getParameter("category");
}
}
catch(Exception e)
{
logger.log("Invalid form category or name, EXCEPTION: "+e);
throw new FileNotFoundException("FILE_NOT_FOUND");
}