问题描述
这是我最初尝试使用 JMockIt 时发现的.我必须承认,我发现 JMockIt 文档对于它提供的内容非常简洁,因此我可能遗漏了一些东西.尽管如此,这是我的理解:
This is what I found from my initial attempts to use JMockIt. I must admit that I found the JMockIt documentation very terse for what it provides and hence I might have missed something. Nonetheless, this is what I understood:
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 声称它可以做任何事情其他模拟框架做的还有很多.好像不是这里的情况
I really want to be proven wrong here. JMockit claims that it can do everything thatother mocking frameworks do plus a lot more. Does not seem like the case here
@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 中没有等效项,正如您从上面的示例中看到的那样
in Mockito, does not have an equivalent in JMockIt as you can see from the example above
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;
}
};
推荐答案
事实上,all 模拟 API 默认模拟或存根模拟类型中的每个方法.我认为您将 mock(type
) ("full" mocking) 与 spy(obj)
(partial mocking 混淆了>).
In fact, all mocking APIs mock or stub out every method in the mocked type, by default. I think you confused mock(type
) ("full" mocking) with spy(obj)
(partial mocking).
JMockit 可以做到这一切,在每种情况下都使用简单的 API.JMockit 教程 中通过示例进行了全部描述.为了证明,您可以查看 示例测试套件(还有更多已从工具包的较新版本中删除),或许多 JMockit 集成测试(目前超过 一千 个).
JMockit does all that, with a simple API in every case. It's all described, with examples, in the JMockit Tutorial.For proof, you can see the sample test suites (there are many more that have been removed from newer releases of the toolkit), or the many JMockit integration tests (over one thousand currently).
相当于 Mockito 的 spy
是动态部分模拟".在 JMockit 中.只需将要部分模拟的实例作为参数传递给 Expectations
构造函数.如果没有记录期望,则在执行被测代码时执行真实代码.顺便说一句,Mockito 在这里有一个严重的问题(JMockit 没有),因为它总是 执行真正的代码,即使在 when(...)
或验证(...)
;正因为如此,人们不得不使用 doReturn(...).when(...)
来避免对间谍对象的意外.
The equivalent to Mockito's spy
is "dynamic partial mocking" in JMockit. Simply pass the instances you want to partially mock as arguments to the Expectations
constructor. If no expectations are recorded, the real code will be executed when the code under test is exercised. BTW, Mockito has a serious problem here (which JMockit doesn't), because it always executes the real code, even when it's called inside when(...)
or verify(...)
; because of this, people have to use doReturn(...).when(...)
to avoid surprises on spied objects.
关于调用验证,JMockit Verifications API 比任何其他 API 都强大得多.例如:
Regarding verification of invocations, the JMockit Verifications API is considerably more capable than any other. For example:
new VerificationsInOrder() {{
// preceding invocations, if any
mockDatabase.save((Article) withNotNull()); times = 2;
// later invocations, if any
}};
这篇关于主要区别:Mockito 和 JMockIt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!