gpt4 book ai didi

java - 测试失败并显示 "when() requires an argument which has to be ' 对 mock 的方法调用“maven 项目错误(未使用框架)

转载 作者:行者123 更新时间:2023-12-05 06:21:17 26 4
gpt4 key购买 nike

您好,我正在尝试在我的数据库中模拟插入并运行它进行测试。测试失败并出现错误“when() 需要一个必须是‘模拟方法调用’的参数”。我是新手,需要一些帮助。

我没有使用任何框架

编辑测试代码

保存Servlet.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;
}
}

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

}
}

堆栈跟踪

org.mockito.exceptions.misusing.MissingMethodInvocationException: 

when() 需要一个参数,该参数必须是“对模拟的方法调用”。例如: 当(mock.getArticles()).thenReturn(文章);

此外,此错误可能会出现,因为:1. 你 stub :final/private/equals()/hashCode() 方法之一。 这些方法不能被 stub /验证。 不支持在非公共(public)父类上声明的模拟方法。2. 在 when() 中,你不是在 mock 上调用方法,而是在其他一些对象上调用方法。

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)

最佳答案

在异常消息中它说:

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

在你的代码中你有:

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

我不太确定你想用第二行实现什么,但它会导致错误并且没有用 - 你应该删除它。

其实整个测试我都不清楚。

如果我理解正确的话,insert 是被测试的对象,所以没有必要模拟它,你必须在一个实际的对象上工作。

关于java - 测试失败并显示 "when() requires an argument which has to be ' 对 mock 的方法调用“maven 项目错误(未使用框架),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59929225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com