英文:
Can't Mock Anything in Mockito
问题
我是新手单元测试,尝试使用Mockito模拟一个映射。当我尝试模拟ConcurrentHashMap时,它会抛出如下错误。不仅是HashMap,它对所有对象都抛出错误。
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.concurrent.ConcurrentHashMap;
import static org.mockito.Mockito.mock;
class UtilsTest {
@BeforeEach
void setUp() {
ConcurrentHashMap mockedMap = mock(ConcurrentHashMap.class);
}
@Test
void lessLoadedServersCount(){
}
}
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class java.util.concurrent.ConcurrentHashMap.
Mockito can only non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 11
JVM vendor name : OpenLogic
JVM vendor version : 11.0.19+7-adhoc.admin.jdk11u
JVM name : OpenJDK 64-Bit Server VM
JVM version : 11.0.19+7-adhoc.admin.jdk11u
JVM info : mixed mode
OS name : Mac OS X
OS version : 13.2.1
Underlying exception : java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
尝试使用列表和集合也会收到相同的错误。我尝试使用JDK 19也会得到相同的错误。
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class java.util.ArrayList.
Mockito can only non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class java.util.HashSet.
Mockito can only non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
英文:
I'm new to Unit testing and Tried to Mock a map using Mockito.
When I tried to mock ConcurrentHashMap it throws error like this . Not only Hash map it throws error for all Object .
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.concurrent.ConcurrentHashMap;
import static org.mockito.Mockito.mock;
class UtilsTest {
@BeforeEach
void setUp() {
ConcurrentHashMap mockedMap = mock(ConcurrentHashMap.class);
}
@Test
void lessLoadedServersCount(){
}
}
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class java.util.concurrent.ConcurrentHashMap.
Mockito can only non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 11
JVM vendor name : OpenLogic
JVM vendor version : 11.0.19+7-adhoc.admin.jdk11u
JVM name : OpenJDK 64-Bit Server VM
JVM version : 11.0.19+7-adhoc.admin.jdk11u
JVM info : mixed mode
OS name : Mac OS X
OS version : 13.2.1
Underlying exception : java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
Tried With List and Sets as well getting same error . I tried with JDK 19 getting same error.
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class java.util.ArrayList.
Mockito can only non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class java.util.HashSet.
Mockito can only non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
答案1
得分: 2
我使用Mockito的哪个版本?
我在这个GitHub仓库中创建了一个示例,对ConcurrentHashMap
进行模拟似乎运行正常。
class UtilTest {
private ConcurrentHashMap map;
@BeforeEach
void setUp() {
map = mock(ConcurrentHashMap.class);
}
@Test
void test(){
when(map.get("key")).thenReturn("value");
assertEquals("value", map.get("key"));
}
}
英文:
Which version of Mockito do you use?
I create an example in this GitHub repository and mocking ConcurrentHashMap
seems to work fine.
class UtilTest {
private ConcurrentHashMap map;
@BeforeEach
void setUp() {
map = mock(ConcurrentHashMap.class);
}
@Test
void test(){
when(map.get("key")).thenReturn("value");
assertEquals("value", map.get("key"));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论