我正在使用 Mockito 来进行一些模拟/测试。我的场景很简单:我有一个使用 mock()
模拟的类,并且我(间接)调用了这个类很多次(即~100k)
Mockito 似乎为每次调用保存了一些数据,所以我在某个时间点耗尽了内存。
我想告诉 mockito 不要保存任何数据(我不打算调用 verify()
等,我只是不关心,对于这个特定的测试,到达了什么到那个模拟)。我不想在每次调用时都创建新的模拟。
最佳答案
您可以使用 Mockito.reset(mock)
,请注意,在您调用它之后,您的 mock 将忘记所有 stub 以及所有交互,因此您需要重新设置它. Mockito's documentation on the method有这些使用说明:
List mock = mock(List.class);
when(mock.size()).thenReturn(10);
mock.add(1);
reset(mock);
//at this point the mock forgot any interactions & stubbing
他们也不鼓励使用这种方法,就像对你的问题的评论一样。通常这意味着您可以重构您的测试以更加专注:
Instead of reset() please consider writing simple, small and focused test methods over lengthy, over-specified tests. First potential code smell is reset() in the middle of the test method. This probably means you're testing too much. Follow the whisper of your test methods: "Please keep us small & focused on single behavior". There are several threads about it on mockito mailing list.
关于java - Mockito mock() 和调用记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19163378/