无法在Mockito中模拟任何东西。

huangapple go评论137阅读模式
英文:

Can't Mock Anything in Mockito

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对单元测试还不熟悉,尝试使用Mockito来模拟一个map。当我尝试模拟ConcurrentHashMap时,它会抛出如下错误。不仅对HashMap,对所有对象都会抛出错误。

  1. import org.junit.jupiter.api.BeforeEach;
  2. import org.junit.jupiter.api.Test;
  3. import java.util.concurrent.ConcurrentHashMap;
  4. import static org.mockito.Mockito.mock;
  5. class UtilsTest {
  6. @BeforeEach
  7. void setUp() {
  8. ConcurrentHashMap mockedMap = mock(ConcurrentHashMap.class);
  9. }
  10. @Test
  11. void lessLoadedServersCount(){
  12. }
  13. }
  1. org.mockito.exceptions.base.MockitoException:
  2. Mockito无法模拟这个类:class java.util.concurrent.ConcurrentHashMap
  3. Mockito只能模拟非私有且非final的类。
  4. 如果您不确定为什么会出现此错误,请报告给邮件列表。
  5. Java : 11
  6. JVM厂商名称 : OpenLogic
  7. JVM厂商版本 : 11.0.19+7-adhoc.admin.jdk11u
  8. JVM名称 : OpenJDK 64-Bit Server VM
  9. JVM版本 : 11.0.19+7-adhoc.admin.jdk11u
  10. JVM信息 : 混合模式
  11. 操作系统名称 : Mac OS X
  12. 操作系统版本 : 13.2.1
  13. 底层异常 : java.lang.UnsupportedOperationException: 无法使用反射定义类:找不到sun.misc.Unsafe

尝试使用List和Set也会得到相同的错误。我尝试使用JDK 19也得到相同的错误。

  1. org.mockito.exceptions.base.MockitoException:
  2. Mockito无法模拟这个类:class java.util.ArrayList
  3. Mockito只能模拟非私有且非final的类。
  4. 如果您不确定为什么会出现此错误,请报告给邮件列表。
  1. org.mockito.exceptions.base.MockitoException:
  2. Mockito无法模拟这个类:class java.util.HashSet
  3. Mockito只能模拟非私有且非final的类。
  4. 如果您不确定为什么会出现此错误,请报告给邮件列表。
英文:

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 .

  1. import org.junit.jupiter.api.BeforeEach;
  2. import org.junit.jupiter.api.Test;
  3. import java.util.concurrent.ConcurrentHashMap;
  4. import static org.mockito.Mockito.mock;
  5. class UtilsTest {
  6. @BeforeEach
  7. void setUp() {
  8. ConcurrentHashMap mockedMap = mock(ConcurrentHashMap.class);
  9. }
  10. @Test
  11. void lessLoadedServersCount(){
  12. }
  13. }
  1. org.mockito.exceptions.base.MockitoException:
  2. Mockito cannot mock this class: class java.util.concurrent.ConcurrentHashMap.
  3. Mockito can only non-private & non-final classes.
  4. If you're not sure why you're getting this error, please report to the mailing list.
  5. Java : 11
  6. JVM vendor name : OpenLogic
  7. JVM vendor version : 11.0.19+7-adhoc.admin.jdk11u
  8. JVM name : OpenJDK 64-Bit Server VM
  9. JVM version : 11.0.19+7-adhoc.admin.jdk11u
  10. JVM info : mixed mode
  11. OS name : Mac OS X
  12. OS version : 13.2.1
  13. 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.

  1. org.mockito.exceptions.base.MockitoException:
  2. Mockito cannot mock this class: class java.util.ArrayList.
  3. Mockito can only non-private & non-final classes.
  4. If you're not sure why you're getting this error, please report to the mailing list.
  1. org.mockito.exceptions.base.MockitoException:
  2. Mockito cannot mock this class: class java.util.HashSet.
  3. Mockito can only non-private & non-final classes.
  4. If you're not sure why you're getting this error, please report to the mailing list.

答案1

得分: 2

我们使用的是Mockito的哪个版本?

我在这个GitHub仓库中创建了一个示例,对ConcurrentHashMap进行模拟似乎运行良好。

  1. class UtilTest {
  2. private ConcurrentHashMap map;
  3. @BeforeEach
  4. void setUp() {
  5. map = mock(ConcurrentHashMap.class);
  6. }
  7. @Test
  8. void test(){
  9. when(map.get("key")).thenReturn("value");
  10. assertEquals("value", map.get("key"));
  11. }
  12. }
英文:

Which version of Mockito do you use?

I create an example in this GitHub repository and mocking ConcurrentHashMap seems to work fine.

  1. class UtilTest {
  2. private ConcurrentHashMap map;
  3. @BeforeEach
  4. void setUp() {
  5. map = mock(ConcurrentHashMap.class);
  6. }
  7. @Test
  8. void test(){
  9. when(map.get("key")).thenReturn("value");
  10. assertEquals("value", map.get("key"));
  11. }
  12. }

huangapple
  • 本文由 发表于 2023年8月9日 14:58:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定