How to initialize servlet configuration with mockito… here is a solution to the problem.
How to initialize servlet configuration with mockito
I’m writing test cases for servlets. In my tests, I can’t initialize the Servlet Config variable. I also provide my code and tests
The code is:
public void service (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
ServletConfig config = getServletConfig();
msSmtpServer = getServletConfig().getInitParameter("smptserver");
msSmtpPort = getServletConfig().getInitParameter("smtpport");
msUserName = getServletConfig().getInitParameter("username");
msPassword = getServletConfig().getInitParameter("password");
String buf = getRequestBuffer(request).toString();
The test is:
public class AddUserServletTest {
@Mock
HttpServletRequest request;
@Mock
HttpServletResponse response;
@Mock
HttpSession session;
@Mock
RequestDispatcher rd;
@Mock
ServletOutputStream servletOut;
@Mock
ServletConfig sg;
public final static String Seperator = new Character((char)1).toString();
public final static String ContentDelimeter = new Character((char)2).toString();
@BeforeClass
public void setUp() throws Exception
{
MockitoAnnotations.initMocks(this);
}
@Test
public void test() throws Exception {
DataSet ds=new DataSet();
String buffer=ds.getUserId()+Seperator+ds.getTableID()+Seperator+ds.getMemberID()+Seperator+ds.getNHID();
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
ZipOutputStream out = new ZipOutputStream(bos);
out.putNextEntry(new ZipEntry("request.txt"));
out.write(buffer.getBytes("UTF-8"));
out.closeEntry();
out.close ();
bos.close();
String b64String = Base64.encodeBase64String(bos.toByteArray());
Reader inputString = new StringReader(b64String);
BufferedReader reqbuffer = new BufferedReader(inputString);
when(request.getReader()).thenReturn(reqbuffer);
when(sg.getInitParameter("smptserver")).thenReturn("abc.xyz.com");
when(sg.getInitParameter("smtpport")).thenReturn("80");
when(sg.getInitParameter("username")).thenReturn("[email protected]");
when(sg.getInitParameter("password")).thenReturn("abcd");
when(response.getOutputStream()).thenReturn(servletOut);
new AddUsers().service(request, response);
ArgumentCaptor<String> bufferCaptor = ArgumentCaptor.forClass(String.class);
verify(servletOut).print(bufferCaptor.capture());
String responseBody = bufferCaptor.getValue();
ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(Base64.decodeBase64(responseBody.toString().getBytes())));
zipIn.getNextEntry();
StringBuffer sb = new StringBuffer();
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
IOUtils.copy(zipIn, out1);
sb = new StringBuffer();
sb.append(out1.toString("UTF-8"));
System.out.println("--------------- :"+sb.toString());
String[] res=sb.toString().split(Seperator);
AssertJUnit.assertEquals("Success",res[0]);
}
}
I tried initializing, but in the servlet the value is empty and no value is fetched.
How do I initialize the ServletConfig variable in a test using mockito?
Solution
Current code:
new AddUsers().service(request, response);
Create a new servlet instance and try to use it immediately, so the instance doesn’t know about sg
.
Try this to allow injecting simulations before use:
AddUsers servlet = new AddUsers();
servlet.init(sg); use the servlet life-cycle method to inject the mock
servlet.service(request, response);