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

615 Views Asked by At

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



   
1

There are 1 best solutions below

0
just me On BEST ANSWER

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("https://example.com/test/test");
Mockito.when(httpServletRequest.getContextPath()).thenReturn("https://example.com/");

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