使用mockito获取req.getRequestURI()时出现异常。

huangapple go评论85阅读模式
英文:

Getting exception while using mockito to get req.getRequestURI()

问题

以下是翻译后的内容:

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());

// 确定最后一个斜杠
// 右侧是表单名称
// 左侧是类别
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, "在查询字符串中重新配置的表单名称 reqURI25 = " + reqURI25);
    }
}

if ((lastSlash + 1) == uriLen)
{
    lastSlash = reqURI25.substring(0, uriLen - 1).lastIndexOf("/");
    skipLastChar = true;
}

logger.debug(method, "lastSlash = " + lastSlash);

// category=req.getParameter("category"); // 旧的2.0引擎
// 处理如果lastSlash = 0,没有类别的情况

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("无效的表单类别或名称,异常:" + e);
    throw new FileNotFoundException("FILE_NOT_FOUND");
}

请注意,我只翻译了您提供的代码部分,没有包括原始英文内容。如果您有任何进一步的翻译需求,请随时告诉我。

英文:

I am facing issue while mocking a requestURI().any help in this issue will be benifical
i am trying to mock using mockito below line

String reqURI25 = req.getRequestURI().substring(req.getContextPath().length());  

I tried with

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);

exception - i am getting 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");
}
</details>
# 答案1
**得分**: 2
看起来你试图一次性模拟太多内容,你需要逐个执行每个方法调用,就像这样(可能需要微调以修复一些错位错误,但你能理解这个思路):
```java
Mockito.when(httpServletRequest.getRequestURI()).thenReturn("https://example.com/test/test");
Mockito.when(httpServletRequest.getContextPath()).thenReturn("https://example.com/");

从某种角度来说,这实际上是一件好事,因为现在你还在测试该行中的子字符串逻辑。

英文:

It looks like you're trying to mock too much in one go, you need to do each method call individually, like (might need to tweak it for off-by-one errors, but you get the idea):

Mockito.when(httpServletRequest.getRequestURI()).thenReturn(&quot;https://example.com/test/test&quot;);
Mockito.when(httpServletRequest.getContextPath()).thenReturn(&quot;https://example.com/&quot;);

This is actually a good thing in a way, because you're now testing the substring logic in that line as well.

huangapple
  • 本文由 发表于 2020年7月23日 21:46:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63055783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定