Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo


How to mock the methods inside doPost() ?

  • Hi, I'm facing problem in testing the doPost() which has a method call in the implementation class.

    public void testDoPostHttpServletRequestHttpServletResponse() throws IOException, InvalidPayLoadException, DBException, ServletException {



    VendorTransactionLog vendorTransactionLog = mock(VendorTransactionLog.class);
    DSCECResponse res = mock(DSCECResponse.class);
    verifyOutputContains("Successfully Updated the Status");


    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
            VendorTransactionLog vendorTransactionLog = new VendorTransactionLog();
                    DataInputStream objDataInputStream = null;
                    String inputXML = null;
                    String strDsRequest = null;
                    StringBuffer stringBuffer = new StringBuffer();
                    BufferedReader bufReader = null;
                    //      if(req.getScheme().equals(PROTOCOL)){
                                    if (inputXML != null && !"".equals(inputXML)){
                                                    try {
                                                            new VendorGateway().dsStatusUpdate(vendorTransactionLog,"VENDOR");
                                                    } catch (Exception e) {
                                                            // TODO Auto-generated catch block
                                                     writeResponse(res, "Update status failed!");
                                                    writeResponse(res, "Successfully Updated the Status");
                                            catch(Exception dbException){
                                                    //ErrorLog.write("Inseide the DSException While DsStatusUpdate()");
                                                    writeResponse(res, "Update status failed!");
                                    else {
                                            writeResponse(res, "Empty request. Update status failed!");
                                            //EventLog.write("Empty request for status update");
                    //      }else{
                    //              writeResponse(res, "UnAuthorised Access to the secured service…..");
                    //      }
                    }catch(Exception e){
                            //ErrorLog.write("Exception while processing the request "+e);
                            writeResponse(res, "Error while processing the request.Update status failed!");

    In test method when doPost is called the res in "when(vendor.dsStatusUpdate(vendorTransactionLog,"VENDOR")).thenReturn(res);" should be returned as per my knowledge. But instead it is trying to call the real implementation "new VendorGateway().dsStatusUpdate(vendorTransactionLog,"VENDOR");". And getting application exception which is obvious to take place.

    Is that Methods inside doPost() cannot be mocked?

    Can someone help me in tackling this issue?

    Thanks for your patience to go through the above code.

  • Alwin Ibba
    Alwin Ibba

    Your problem is not related to Mockrunner. Mockrunner provides an environment to run servlets outside a servlet container. It does not mock arbitrary classes like VendorGateway. The methods mock(), when(), thenReturn() are not Mockrunner methods. It seems you are using Mockito or a similar framework besides Mockrunner and you should search for an answer there.

  • Hi Alwin thanks for the reply. Yes i'm using mokito to mock the objects. What i'm trying to do is that, i'm using mockrunner to test the servlet. While Testing the doPost() method , i have a method (from VendorGateway) and i need to mock this method when doPost is called. So i tried using the Mockito. So is there any suggestion of how i'll achieve this? The real implemention of VendorGateway should not be called.

  • Alwin Ibba
    Alwin Ibba

    Maybe you can refactor the servlet so you can inject a MockVendorGateway and refactor the code to create a VendorGateway if nothing is injected by default. In the test you get the instance of the deployed servlet (createServlet returns it) and inject your MockVendorGateway. The MockVendorGateway can be handwritten or created by some Mock framework. Or you refactor your servlet to take the class name of the VendorGateway as an init parameter, create the VendorGateway with the context classloader, use the real VendorGateway in the production web.xml and use a MockVendorGateway in the test (Mockrunner lets you configure the init parameter with its API). Just some ideas, like mentioned it's not related to Mockrunner but a general problem.

  • Thanks for the reply, i'm considering what you have shared me. I need to pass the mockVendorGateWay to the servlet, instead of creating the new instance in doPost() method.