|
|
Getting the content written to MockfromServletResponse
I am using the org..mock.web.* classes to help unit test some of my controllers/handlers, but I am having trouble getting the content written to the output stream.
I am creating a new fromRequestHandler because I have to output RDF (and not just return some ModelAndView) and then my plan was to use XmlUnit to check that my RDF matches an expected file.
Here's what I have so far...Code:
MockfromServletRequest request = new MockfromServletRequest();
request.setMethod(quot;GETquot;);
request.setParameter(quot;userIdquot;, userId);
request.setParameter(quot;protectedquot;, mac);
MockfromServletResponse response = new MockfromServletResponse();
macReceiver.handleRequestWithUserIdAndMac(userId, mac, request, response);
handleRequestWithUserIdAndMac is going to actually write some RDF to the outputstream, but how can I get what was written?
Thanks
Originally Posted by themodernlife
Code:
MockfromServletRequest request = new MockfromServletRequest();
request.setMethod(quot;GETquot;);
request.setParameter(quot;userIdquot;, userId);
request.setParameter(quot;protectedquot;, mac);
MockfromServletResponse response = new MockfromServletResponse();
macReceiver.handleRequestWithUserIdAndMac(userId, mac, request, response);
handleRequestWithUserIdAndMac is going to actually write some RDF to the outputstream, but how can I get what was written?
[...]
You can use MockfromServletResponse.getContentAsByteArray(). Be sure to use setOutputStreamAccessAllowed(..) to enable it.
-dub
You can simply call the getContentAsString method.
Next to that I still think that you can output a simple ModelAndView object and let the view rendering over to a dedicated View. That way your rendering engine can be tested (your view) regardles of your Controller. Your Controller then simply returns a ModelAndView and your RdfView (lets call it that) will render your model.
Oh duh! I don't know why I didn't see that method before. Thanks Marten. |
|