微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

junit – 主要区别:Mockito和JMockIt

这是我从我最初尝试使用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
}};

原文地址:https://www.jb51.cc/java/122225.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐