I am writing test case for servlets. In Test I am not able to intiallize Servlet Config variable. I am providing my code and test also
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();
and 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 to initialize but in servlet that values are null not getting values.
How to initialize ServletConfig variable in test using mockito?
The current code:
new AddUsers().service(request, response);
creates a new instance of the servlet and attempts to use it immediately so the instance has no awareness of sg.
Try this which allows the mock to be injected before using it:
AddUsers servlet = new AddUsers();
servlet.init(sg); // use the servlet life-cycle method to inject the mock
servlet.service(request, response);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With