'Test Fails with "when() requires an argument which has to be 'a method call on a mock' " error on maven project(no frameworks used)

Hi I am trying to mock an insertion in my database and run it for a test. The test is failing with and error "when() requires an argument which has to be 'a method call on a mock'." I am new to this and i need a little help with it.

I am not using any framework

Editing the test code

SaveServlet.java

package com.consentServlets;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/SaveServlet")
public class SaveServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        //Getting the attributes from the UI
        String first_Name = request.getParameter("fname");
        String last_Name = request.getParameter("lname");
        String gender = request.getParameter("gender");
        String age = request.getParameter("age");
        String dob = request.getParameter("dob");

        //Setting the objects to insert the achieved attributes to corresponding the columns of the table
        patient addPatient = new patient();
        addPatient.setLastName(last_Name);
        addPatient.setFirstName(first_Name);
        addPatient.setGender(gender);
        addPatient.setAge(age);
        addPatient.setDoB(dob);

        out.print(" <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css\">");
        //calling the save function from the patientDao class to execute the query
        int status=patientDao.save(addPatient);
        if(status>0){
            out.print("<p>Patient Record saved successfully!</p>");
            request.getRequestDispatcher("index.html").include(request, response);
        }else{
            out.println("Sorry! unable to save record");
        }

        out.close();
    }

}

patient.java

 package com.consentServlets;
    import java.util.List;
    //creating objects for patient class which will help to store the patient details
    public class patient {  
        private int id;  
        private String first_Name,last_Name,gender,age,dob;  
        public int getId() {  
            return id;  
        }  
        public void setId(int id) {  
            this.id = id;  
        }  
        public String getFirstName() {   
            return first_Name;  
        }  
        public void setFirstName(String first_Name) {  
            this.first_Name = first_Name;  
        }  
        public String getLastName() {  
            return last_Name;  
        }  
        public void setLastName(String last_Name) {  
            this.last_Name = last_Name;  
        }  
        public String getGender() {  
            return gender;  
        }  
        public void setGender(String Gender) {  
            this.gender = Gender;  
        }  
        public String getAge() {  
            return age;  
        }  
        public void setAge(String Age) {  
            this.age = Age;  
        }  
        public String getDoB() {  
            return dob;  
        }  
        public void setDoB(String DOB) {  
            this.dob = DOB;  
        }
    }  

patientDao.java

package com.consentServlets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List; 

public class patientDao {
    //establishing the connection with the database
    public  Connection getConnection(){  
        Connection con=null;  
        try{  
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/consent_share","root","");
        }catch(Exception e){System.out.println(e);}  
        return con;  
    }  
    public static int save(patient addPatient){  
        int status = 0;  
        //Inserting patient details from UI to Database
        try{                            
            Connection con = new patientDao().getConnection();  
            PreparedStatement ps = con.prepareStatement(  
                    "insert into patient(last_name,first_name,gender,age,dob) values (?,?,?,?,?)");  
            ps.setString(1,addPatient.getLastName());  
            ps.setString(2,addPatient.getFirstName());  
            ps.setString(3,addPatient.getGender());  
            ps.setString(4,addPatient.getAge());
            ps.setString(5,addPatient.getDoB());

            status = ps.executeUpdate();  

            con.close();  
        }catch(Exception ex){ex.printStackTrace();}  

        return status;  
    }  

    public static patient getPatientbyId(int id){  
        patient getPatient = new patient();  
        //selecting a patient record by matching the patient_ID 
        try{ 
            Connection con = new patientDao().getConnection();  
            PreparedStatement ps = con.prepareStatement("select * from patient where patient_id=?");  
            ps.setInt(1,id);  
            ResultSet rs = ps.executeQuery();  
            if(rs.next()){  
                getPatient.setId(rs.getInt(1));  
                getPatient.setLastName(rs.getString(2));  
                getPatient.setFirstName(rs.getString(3));  
                getPatient.setGender(rs.getString(4));  
                getPatient.setAge(rs.getString(5));
                getPatient.setDoB(rs.getString(6)); 
            }  
            con.close();  
        }catch(Exception ex){ex.printStackTrace();}  

        return getPatient;
    }  
    // Fetching all the records from table
    public static List<patient> getAllPatients(){  
        List<patient> list = new ArrayList<patient>();  

        try{  
            Connection con = new patientDao().getConnection();  
            PreparedStatement ps = con.prepareStatement("select * from patient");  
            ResultSet rs = ps.executeQuery();  
            while(rs.next()){  
                patient getAllPatients=new patient();  
                getAllPatients.setId(rs.getInt(1));  
                getAllPatients.setFirstName(rs.getString(3));  
                getAllPatients.setLastName(rs.getString(2));  
                getAllPatients.setGender(rs.getString(4));  
                getAllPatients.setAge(rs.getString(5));
                getAllPatients.setDoB(rs.getString(6));   
                list.add(getAllPatients);  
            }  
            con.close();  
        }catch(Exception e){e.printStackTrace();}  

        return list;  
    }  
}  

patientTest.java

    package com.test;

import static org.junit.Assert.assertEquals;

import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import com.consentServlets.patient;
import com.consentServlets.patientDao;

@RunWith(MockitoJUnitRunner.class)

public class patientTest{

    @Mock
    Connection mockCom; 

    @Mock
    PreparedStatement mockSt;

    @InjectMocks
    patientDao mockPatDao;



    @Test
    public void testPatientServlet() throws Exception {


        HttpServletRequest request = Mockito.mock(HttpServletRequest.class);       
        HttpServletResponse response = Mockito.mock(HttpServletResponse.class);    

        /*
         * Mockito.when(request.getParameter("fname")).thenReturn("Rahul");
         * Mockito.when(request.getParameter("lname")).thenReturn("Sen");
         * Mockito.when(request.getParameter("gender")).thenReturn("Male");
         * Mockito.when(request.getParameter("age")).thenReturn("20");
         * Mockito.when(request.getParameter("dob")).thenReturn("1996-07-24");
         */

        patient patientSave = new patient();
        patientSave.setLastName("Rahul");
        patientSave.setFirstName("Sen");
        patientSave.setGender("Male");
        patientSave.setAge("20");
        patientSave.setDoB("1996-07-24");

        //String query = "";

    Mockito.when(new patientDao().getConnection()).thenReturn(mockCom);
        Mockito.when(mockCom.prepareStatement(Matchers.anyString())).thenReturn(mockSt);
        Mockito.when(mockSt.executeUpdate()).thenReturn(1);

        String URI = "/SaveServlet";
        //int status=patientDao.save(patientSave);
        int actualStatus= mockPatDao.save(patientSave);
        int expectedStatus = 1;

        //Mockito.when(insert.save(patientSave)).thenReturn(1);
        assertEquals(expectedStatus,actualStatus);

    }
}

STACKTRACE

org.mockito.exceptions.misusing.MissingMethodInvocationException: 

when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods. Those methods cannot be stubbed/verified. Mocking methods declared on non-public parent classes is not supported. 2. inside when() you don't call method on mock but on some other object.

at com.test.patientTest.testPatientServlet(patientTest.java:62)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:46)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:77)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:83)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)


Solution 1:[1]

In the exception message it says:

inside when() you don't call method on mock but on some other object.

And in your code you have:

patientDao insert = new patientDao();
...
Mockito.when(insert.save(patientSave)).thenReturn(status);

I'm not really sure what you wanted to achieve with that second line but it causes the error and is useless - you should remove it.

Actually the whole test is unclear to me.

If I understand correctly, insert is meant to be the tested object so no point in mocking it, you have to work on an actual object.

Solution 2:[2]

As the stacktrace says when can only be used with mocked objects.

when() requires an argument which has to be 'a method call on a mock'.

While following code has a usage of when where the object is not mocked.

patientDao insert = new patientDao();

Mockito.when(insert.save(patientSave)).thenReturn(status);

You need to mock the insert object OR remove Mockito.when(insert.save(patientSave)).thenReturn(status); as it is not needed.

Solution 3:[3]

When you @InjectMocks, Mockito doesn't create a mock. It creates an actual instance, hence the error:

when() requires an argument which has to be 'a method call on a mock'.

Now the error makes sense. This means when() fails because your class is not a mock but an actual instance.

You can solve this by putting @Spy on top of your @InjectMocks annotation.

I found this out from this article: https://mkyong.com/spring-boot/mockito-when-requires-an-argument-which-has-to-be-a-method-call-on-a-mock/

Solution 4:[4]

If you use KOIN, include in the gradle's dependencies:

dependencies {
    ...
    testImplementation "org.koin:koin-test:2.0.0"
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Mike Cheel
Solution 4 J. Soares