mercredi 7 décembre 2011

How to mock httpRequest with jMockit

/**
* test getMultiple param
* @param request
*/
@Test
public void testGetMultipleParams(final HttpServletRequest request) {

new NonStrictExpectations()
{
// A "local" mock field ("@Mocked" is implied when not annotated):
{
request.getParameter("feed1"); result = "http://feed1";
request.getParameter("feed2"); result = "http://feed2";
request.getParameter("feed3"); result = "http://feed3";
request.getParameter("feed4"); result = null;
}
};

ArrayList toto = RssAggregatorServlet.getMultipleParams(request, "feed");
assertThat(toto.size(),equalTo(3));

assertThat(toto.toArray(new String[0]),equalTo(new String[]{"http://feed1","http://feed2","http://feed3"}));

Aucun commentaire: