3

I am writing a Spring based Test case for my Spring Controller, in my controller we are using like this "request.getSession().getServletContext().getRealPath("/myFolderName");" Its working fine for normal requests, but for unit test cases like MockHttpServletRequest it is the above method call is giving null. how can i get realPath for MockHttpServletRequest.

Thanks, Praneeth.

  • Split the statement up into several smaller ones so you can see which part is causing the NPE. Or use a debugger. – skaffman Apr 23 '12 at 09:46

1 Answers1

2

By default MockHttpServletRequest acts as if root webapp folder is a classpath, therefore you should be able to use getRealPath() for classpath resources.

Alternatively, you can supply MockHttpServletRequest with MockServletContext, and MockServletContext can be configured with specific root folder for getRealPath(), etc.

axtavt
  • 239,438
  • 41
  • 511
  • 482