我没有使用任何框架
编辑测试代码
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();
}
}
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;
}
}
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;
}
}
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:
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)
在异常消息中,它表示:
在when()中,您不是在mock上调用method,而是在其他对象上调用method。
在您的代码中,您有:
patientDao insert = new patientDao();
...
Mockito.when(insert.save(patientSave)).thenReturn(status);
不幸的是: TweetServiceTest.java Hibernateutil.java
编辑测试代码 保存服务器.java patient.java
我正在编写一个类的单元测试,但我得到了一个错误 when()需要一个参数,该参数必须是模拟上的方法调用 错误:
当我尝试运行代码时,它会导致我遇到一个异常。 LoginController.java sampleClass.java MissingMethodInvocationException:when()需要一个必须是“mock上的方法调用”的参数。例如:when(mock.getarticles()).ThenReturn(articles); 此外,出现此错误的原因可能是:1。您使用final/p
问题内容: 我有一个使用Mockito和Spring Test框架的非常简单的测试用例。当我做 我得到这个例外。 我尝试使用不同的方法,但继续收到此错误消息。我在Mockito中使用Spring 3.1.0.RELEASE。请分享并指导我正确的方向。 问题答案: 您需要先创建pcUserService的MOCK,然后使用该模拟。
马上,我是一个初学者,所以这个问题可能看起来很初级。 每当我使用“extends”并尝试编译时,我都会得到如下错误消息: 我确实知道参数有问题(在本例中是值为“OLAF”的字符串),但我不明白具体是什么问题。此外,我没有得到第二个错误消息。 正如前面所说,我是一个初学者。我发现了几个类似问题的帖子,但无法理解解决方案。很多人都写过一些关于“super()”的东西。我既不知道这段代码应该做什么,也不