【发布时间】:2025-11-21 09:35:01
【问题描述】:
我试图理解以下场景的测试,其中应该模拟地图的第一次创建并且不应该模拟第二次实例的创建,并且我已经按照以下方式编写了在一个场景中工作而在以后的场景中不工作的方式,
public class A {
public void test(){
Map<String,String> map = new HashMap<String, String>();
Map<String,String> map1 = new HashMap<String, String>();
System.out.println(map);
System.out.println(map1);
}
}
按预期工作的第一种编写测试的方法
@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
public class ATest {
@Test
public void test() throws Exception{
HashMap<String,String> map = PowerMockito.mock(HashMap.class);
HashMap<String,String> hashMap = new HashMap<String, String>();
PowerMockito.whenNew(HashMap.class).withNoArguments().thenReturn(map,hashMap);
A a = new A();
a.test();
}
}
我直接传递 new HashMap() 的第二种方式,抛出异常,
@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
public class ATest {
@Test
public void test() throws Exception{
HashMap<String,String> map = PowerMockito.mock(HashMap.class);
PowerMockito.whenNew(HashMap.class).withNoArguments().thenReturn(map,new HashMap<String,String>());
A a = new A();
a.test();
}
}
谁能告诉我为什么当我直接通过 HashMap 时它不起作用.....?
【问题讨论】:
-
在使用方法链时会发生同样的情况吗?即
.thenReturn(map).thenReturn(new HashMap<String,String>()) -
@Nkosi 方法链接所有实例都分配了模拟 HashMap,链接/添加第二个 thenReturn 没有效果
-
抛出了什么异常。
-
@Nkosi org.mockito.exceptions.misusing.UnfinishedStubbingException:在此处检测到未完成的存根:-> 在 ATest.test(ATest.java:23) 例如thenReturn() 可能会丢失。正确的存根示例:when(mock.isOk()).thenReturn(true); when(mock.isOk()).thenThrow(异常); doThrow(exception).when(mock).someVoidMethod();提示:
-
@Nkosi 认为我创建的 Mock 不是最终的,但它仍在工作..,我们的变量/引用/对象不必是最终的
标签: powermock