我正在尝试使用Junit和Mockito进行单元测试用例来测试我的数据库项目。但是,当我这样做时,我得到一个错误..发布我的代码和堆栈跟踪。我对测试完全陌生。请帮助
注意*-我没有使用任何框架
测试代码
PatientTest.java
package com.test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.io.*;
import javax.servlet.http.*;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.mockito.Mockito;
import com.consentServlets.SaveServlet;
public class patientTest extends Mockito{
@Test
public void testServlet() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
when(request.getParameter("fname")).thenReturn("Rahul");
when(request.getParameter("lname")).thenReturn("Sen");
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
when(response.getWriter()).thenReturn(writer);
new SaveServlet().doPost(request, response);
verify(request, atLeast(1)).getParameter("fname");
writer.flush(); // it may not have been flushed yet...
assertTrue(stringWriter.toString().contains("My expected string"));
}
}
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();
}
}
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 static 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 = 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 = 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 = 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;
}
}
Stacktrace
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: interface javax.servlet.http.HttpServletRequest.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 13
JVM vendor name : Oracle Corporation
JVM vendor version : 13.0.1+9
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 13.0.1+9
JVM info : mixed mode, sharing
OS name : Windows 10
OS version : 10.0
Underlying exception : java.lang.UnsupportedOperationException: Cannot define class using reflection
at com.test.patientTest.testServlet(patientTest.java:17)
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.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.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)
Caused by: java.lang.UnsupportedOperationException: Cannot define class using reflection
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Unavailable.defineClass(ClassInjector.java:821)
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.inject(ClassInjector.java:185)
at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:187)
at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:79)
at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4376)
at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.mockClass(SubclassBytecodeGenerator.java:94)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:37)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:34)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:346)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:161)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:355)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:32)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMockType(SubclassByteBuddyMockMaker.java:71)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:42)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:25)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:63)
at org.mockito.Mockito.mock(Mockito.java:1729)
at org.mockito.Mockito.mock(Mockito.java:1642)
... 24 more
Caused by: java.lang.IllegalArgumentException: Unknown Java version: 13
at net.bytebuddy.ClassFileVersion.ofJavaVersion(ClassFileVersion.java:135)
at net.bytebuddy.ClassFileVersion$VersionLocator$ForJava9CapableVm.locate(ClassFileVersion.java:337)
at net.bytebuddy.ClassFileVersion.ofThisVm(ClassFileVersion.java:147)
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$CreationAction.run(ClassInjector.java:301)
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$CreationAction.run(ClassInjector.java:290)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:312)
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.<clinit>(ClassInjector.java:70)
at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:184)
... 41 more
这几乎可以肯定是由于您使用的Mockito版本由于此错误而尚不支持Java 13
Caused by: java.lang.IllegalArgumentException: Unknown Java version: 13
值得注意的是,不是Mockito本身不支持Java 13,而是用于字节码操作的基础库byte-buddy。
[官方字节码/ Mockito文档似乎并未确切指示哪个版本增加了Java 13支持,但似乎已在bytebuddy 1.9.7中添加了它
Mockito 3.2.2将字节伙伴的依赖关系更新为1.10.5,因此应该足够了。