这是我从我最初尝试使用JMockIt时发现的.我必须承认,我发现JMockIt文档对它提供的内容非常简洁,因此我可能错过了一些.尽管如此,这是我所了解的:
Mockito: List a = mock(ArrayList.class) does not stub out all methods of List.class by default. a.add("foo") is going to do the usual thing of adding the element to the list. JMockIt: @Mocked ArrayList<String> a; It stubs out all the methods of a by default. So,now a.add("foo") is not going to work. This seems like a very big limitation to me in JMockIt. How do I express the fact that I only want you to give me statistics of add() method and not replace the function implementation itself What if I just want JMockIt to count the number of times method add() was called,but leave the implementation of add() as is? I a unable to express this in JMockIt. However,it seems I can do this in Mockito using spy()
我真的想在这里被证明是错的. JMockit声称可以做到这一切
其他嘲笑框架也加了很多.看起来不像这样
@Test public void shouldPersistRecalculatedArticle() { Article articleOne = new Article(); Article articleTwo = new Article(); when(mockCalculator.countNumberOfRelatedArticles(articleOne)).thenReturn(1); when(mockCalculator.countNumberOfRelatedArticles(articleTwo)).thenReturn(12); when(mockDatabase.getArticlesFor("Guardian")).thenReturn(asList(articleOne,articleTwo)); articleManager.updateRelatedArticlesCounters("Guardian"); InOrder inOrder = inOrder(mockDatabase,mockCalculator); inOrder.verify(mockCalculator).countNumberOfRelatedArticles(isA(Article.class)); inOrder.verify(mockDatabase,times(2)).save((Article) notNull()); } @Test public void shouldPersistRecalculatedArticle() { final Article articleOne = new Article(); final Article articleTwo = new Article(); new Expectations() {{ mockCalculator.countNumberOfRelatedArticles(articleOne); result = 1; mockCalculator.countNumberOfRelatedArticles(articleTwo); result = 12; mockDatabase.getArticlesFor("Guardian"); result = asList(articleOne,articleTwo); }}; articleManager.updateRelatedArticlesCounters("Guardian"); new VerificationsInOrder(2) {{ mockCalculator.countNumberOfRelatedArticles(withInstanceOf(Article.class)); mockDatabase.save((Article) withNotNull()); }}; }
这样的声明
inOrder.verify(mockDatabase,times(2)).save((Article) notNull());
在Mockito中,在JMockIt中没有一个等同物,就像上面的例子所示
new NonStrictExpectations(Foo.class,Bar.class,zooObj) { { // don't call zooObj.method1() here // Otherwise it will get stubbed out } }; new Verifications() { { zooObj.method1(); times = N; } };
解决方法
事实上,默认情况下,所有嘲笑的API都会嘲笑或扼杀嘲笑类型的每个方法.我认为你将模拟(类型)(“完全”嘲笑)与间谍(obj)(部分嘲笑)混淆.
在所有情况下,JMockit都会使用简单的API.这一切都在JMockit Tutorial中进行了描述,举例说明.
为了证明,您可以看到sample test suites(还有更多的工具从较新版本中删除,但仍可在old zip files中找到),或许多JMockit集成测试(目前超过一千个).
相当于Mockito的间谍是JMockit中的“动态部分嘲笑”.只需将您想要部分模拟的实例传递给Expectations构造函数的参数.如果没有预期的记录,当执行代码被执行时,实际代码将被执行. BTW,Mockito在这里有一个严重的问题(JMockit没有),因为它总是执行真正的代码,即使当它被调用时(…)或验证(…);因此,人们必须使用doReturn(…).(…)以避免被窥视的对象的惊喜.
关于调用的验证,JMockit验证API比任何其他API都能够更强大.例如:
new VerificationsInOrder() {{ // preceding invocations,if any mockDatabase.save((Article) withNotNull()); times = 2; // later invocations,if any }};