使用mockito-core-2.24.jar和junit,它会给出以下异常
这是纯Java代码,我收到:
org.mockito.exceptions.base.MockitoException:Mockito无法模拟此 class:类javax.servlet.jsp.PageContext。
Mockito只能模拟非私有和非最终类。
Java : 1.8
JVM vendor name : Oracle Corporation
JVM vendor version : 25.202-b08
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 1.8.0_202-b08
JVM info : mixed mode
OS name : Windows 10
OS version : 10.0
异常堆栈跟踪:
Underlying exception : java.lang.IllegalArgumentException: Could not create type
at com.ups.webappcommon.cclamp.cclamptag.Wems_GetContentTagTest.setUp(Wems_GetContentTagTest.java:22)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
at org.mockito.Mockito.mock(Mockito.java:1896)
at org.mockito.Mockito.mock(Mockito.java:1805)
... 14 more
Caused by: java.lang.NoClassDefFoundError: javax/el/ELContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getDeclaredMethods(Unknown Source)
at net.bytebuddy.description.method.MethodList$ForLoadedMethods.
Caused by: java.lang.ClassNotFoundException: javax.el.ELContext
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 49 more
我的代码:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import org.mockito.Mockito;
import junit.framework.TestCase;
public class xxxxxx extends TestCase {
public xxxxxxxxxxxxxxxxx xx = new xxxxxxxxxxxxxxx();
private final PageContext pageContext = Mockito.mock(PageContext.class);
public HttpServletRequest request;
protected void setUp() throws Exception {
super.setUp();
xxxxxxxxx.setPageContext(pageContext);
request = Mockito.mock(HttpServletRequest.class);
xxxxxxxx.setPageContext(pageContext);
Mockito.when(pageContext.getRequest()).thenReturn(request);
}
答案 0 :(得分:1)
这里:
原因:java.lang.NoClassDefFoundError:javax / el / ELContext
那是真正的问题。归结为:项目设置不足。
您必须确保满足您的所有依赖性,甚至包括“传递性”内容。
换句话说:确保您的类路径中有el-api.jar。
并注意:错误消息...
Mockito只能模拟非私有和非最终类。
在这里“无效”,因为该类既不是私有文件也不是文件。但事实是,Mockito一直在打印该消息,因为人们经常尝试这样做:嘲笑私有/最终类。