Menu

MockServletContext, RealPath and init()

2008-09-02
2013-04-15
  • Nobody/Anonymous

    Hi,

    I have a question on understanding/using MockServletContext - I have to test a servlet which contains loads of getServletContext().getRealPath(String) method calls in its init() method. Unfortunately, this value is for some reason null, which of course causes a bunch of problems. I've tried to figure out how this can be done by going thru your examples with no success.

    Now, to the question: To mock the servlet, I need to set/mock the path to the app by setting the path with MockServletContext.setRealPath() to a certain value - right? If so, how can this be done?

    I'm fairly new to using MockRunner, so please forgive me if I trouble you with this rookie question.

    Thanks in advance,
    Sascha.

     
    • Alwin Ibba

      Alwin Ibba - 2008-09-02

      Hi Sascha,

      Yes you are right. You have to set the real path using setRealPath, e.g.

      getWebMockObjectFactory().getMockServletContext().setRealPath("path", "realpath");

      so if the servlet calls getRealPath("path") it gets "realpath" as return value. There's no other way than setting all real path values you want to test manually at the moment. Otherwise you get null.

      Kind regards,

      Alwin

       
    • Nobody/Anonymous

      Hi Alwin,

      thank you for such a lightning fast response! You really helped me a lot.

      Regards,
      Sascha.

       

Log in to post a comment.

MongoDB Logo MongoDB