使用mockito-core-2.24.jar和junit提供以下异常
这是纯Java代码,我收到:
org.mockito.exceptions.base.mockitoException:Mockito无法模拟此 班级:类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);
}
在这里:
引起的:
这是真正的问题。而且归结为:项目设置不足。
您必须确保所有您的依赖项也得到满足,甚至是"及其"及其"的东西。
换句话说:确保您的班级路径中有el-api.jar。
和注意:错误消息...
Mockito只能模拟非私人&非最终类。
在这里是"无效的",因为该类既不是私有的也不是私人文件。但事实是,摩根托(Mockito)一直在打印该消息,人们经常尝试这样做:嘲笑私人/最终课程。