I have to put a mock object in the ServletContext. What I would expect is a public method getServletContext() in ServletRunner (there is a non-public method). Then I could do Black-Box testing (which is what I want) like:
The workaround is lengthy. The routing from the request type (get, post, ...) to the servlet method does not happen automatically. The manipulation of the servlet context has to be done for every test method.
Is there any reason why not offering a public getServletContext method of the ServletRunner?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have to put a mock object in the ServletContext. What I would expect is a public method getServletContext() in ServletRunner (there is a non-public method). Then I could do Black-Box testing (which is what I want) like:
ServletUnitClient sc = sr.newClient();
WebRequest request = new GetMethodWebRequest("http://test.meterware.com/myServlet?a=1&b=2");
WebResponse response = sc.getResponse( request );
Now I have to do the work-around:
WebRequest request = new GetMethodWebRequest(BASE_URL + urlSuffix);
InvocationContext invocationContext = _servletUnitClient.newInvocation(request);
SomeServlet someServlet = (SomeServlet) invocationContext.getServlet();
someServlet.getServletContext().setAttribute(RestServlet.BASE_SERVICE_ATTRIBUTE_KEY,_mockBaseService.proxy());
someServlet.doGet(testClasses.getRequest(),invocationContext.getResponse());
WebResponse response = invocationContext.getServletResponse();
The workaround is lengthy. The routing from the request type (get, post, ...) to the servlet method does not happen automatically. The manipulation of the servlet context has to be done for every test method.
Is there any reason why not offering a public getServletContext method of the ServletRunner?