mockito 测试失败,Maven项目上出现“when()需要一个参数,该参数必须是”mock上的方法调用"“错误(未使用框架)

inn6fuwd  于 2022-11-08  发布在  Maven
关注(0)|答案(4)|浏览(171)

你好,我正在尝试模拟数据库中的一个插入,并运行它进行测试。测试失败,并出现错误“when()需要一个参数,该参数必须是”对模拟的方法调用“。”我是新手,我需要一点帮助。
我没有使用任何框架
编辑测试代码

保存服务小程序.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();
    }

}

患者.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;  
        }
    }

病人道.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;  
    }  
}

患者测试.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);

    }
}

堆栈跟踪

org.mockito.exceptions.misusing.MissingMethodInvocationException:

when()需要一个参数,该参数必须是“对模拟的方法调用”。例如:如果你的文章不存在,那么你就可以使用这个方法。
此外,出现此错误的原因可能是:1.您将以下任何一项存根:final/private/equals()/hashCode()方法。这些方法 * 不能 * 被存根/验证。不支持在非公共父类上声明的模拟方法。2.在when()中,您不对模拟对象调用方法,而是对其他对象调用方法。

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)
gijlo24d

gijlo24d1#

在异常消息中,它表示:
在when()中,您不是在mock上调用方法,而是在其他对象上调用方法。
在您的代码中,您有:

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

我不太确定你想用第二行实现什么,但它会导致错误,是无用的-你应该删除它。
其实整个测试我都不清楚。
如果我没理解错的话,insert应该是被测试的对象,所以没有必要嘲笑它,你必须在一个实际的对象上工作。

hkmswyz6

hkmswyz62#

正如stacktrace所说,when只能与模拟对象一起使用。
when()需要一个参数,该参数必须是“对模拟的方法调用”。
而以下代码使用了when,其中对象未被模拟。
patientDao insert = new patientDao();
Mockito.when(insert.save(patientSave)).thenReturn(status);
您需要模拟insert对象,或者删除不需要的Mockito.when(insert.save(patientSave)).thenReturn(status);

xxhby3vn

xxhby3vn3#

当您@InjectMocks时,Mockito不会创建一个mock,而是创建一个实际的示例,因此出现以下错误:
when()需要一个参数,该参数必须是"mock上的方法调用"。
现在这个错误是有意义的了,这意味着 when() 失败,因为你的类不是一个mock,而是一个实际的示例。
您可以通过将@Spy放在@InjectMocks注解的顶部来解决这个问题。
我从这篇文章中发现了这一点:https://mkyong.com/spring-boot/mockito-when-requires-an-argument-which-has-to-be-a-method-call-on-a-mock/

mpbci0fu

mpbci0fu4#

如果您使用KOIN,请在gradle的依赖项中包括:

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

相关问题