使用 mockito 获取 req.getRequestURI() 时出现异常
Getting exception while using mockito to get req.getRequestURI()
我在模拟 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 for
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");
}
看起来你试图一次模拟太多,你需要单独执行每个方法调用,比如(可能需要调整它以防止错误,但你明白了) :
Mockito.when(httpServletRequest.getRequestURI()).thenReturn("https://example.com/test/test");
Mockito.when(httpServletRequest.getContextPath()).thenReturn("https://example.com/");
这在某种程度上实际上是一件好事,因为您现在也在测试该行中的子字符串逻辑。
我在模拟 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 for
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");
}
看起来你试图一次模拟太多,你需要单独执行每个方法调用,比如(可能需要调整它以防止错误,但你明白了) :
Mockito.when(httpServletRequest.getRequestURI()).thenReturn("https://example.com/test/test");
Mockito.when(httpServletRequest.getContextPath()).thenReturn("https://example.com/");
这在某种程度上实际上是一件好事,因为您现在也在测试该行中的子字符串逻辑。