之间的主要区别:Mockito和JMockIt [英] Major difference between: Mockito and JMockIt

查看:401
本文介绍了之间的主要区别:Mockito和JMockIt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我最初尝试使用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 that other 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)(完整"模拟)与spy(obj)(部分模拟)混淆了.

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来完成所有这些工作.在 JMock教程中通过示例进行了全部描述. 为了证明这一点,您可以查看示例测试套件(还有更多已从工具包的较新版本中删除,但仍可以在旧zip文件中找到),或许多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, but can still be found in the old zip files), or the many JMockit integration tests (over one thousand currently).

与Mockito的spy等效的是JMockit中的动态部分模拟".只需将要部分模拟的实例作为参数传递给Expectations构造函数.如果未记录任何期望,则在执行被测代码时将执行实际代码.顺便说一句,Mockito在这里有一个严重的问题(JMockit没有),因为即使在when(...)verify(...)内部调用它,它也总是执行真正的代码.因此,人们必须使用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比其他任何功能都强大得多.例如:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆