java 存根和模拟时的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14736219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between stub and when in mockito
提问by Ramya
I am new to mockito.
我是mockito的新手。
need to know difference between stub and when
需要知道存根和何时之间的区别
1. stub(cpproxy.getBinList()).toReturn(gettestbins());
2. when(cpproxy.getBinList()).thenReturn(gettestbins());
whats the difference between these two?
这两者有什么区别?
回答by Brice
Actually they are technically the same. When Mockito was first created, we were talking about stubs, so the vocabulary followed that idea. Later people thought it was better to think in interactionsrather that technicalterms, so the vocabulary followed the when ... then ...style. This change in vocabulary helps people to think about interactions, messagingbetween object. Which is the most interesting idea (message passing) thing in an object oriented language (quoting Alan Kay).
实际上,它们在技术上是相同的。当 Mockito 最初被创建时,我们谈论的是存根,所以词汇表遵循这个想法。后来人们认为最好在交互中思考而不是技术术语,因此词汇遵循when ... then ...风格。词汇的这种变化有助于人们思考对象之间的交互和消息传递。这是面向对象语言中最有趣的想法(消息传递)(引用 Alan Kay)。
Nowadays testing approach has evolved to Behavior Driven Development (from Dan North), which is almost the same thing but focus even more on the behavior at design time. To reflect that thinking, people asked Mockito to offer an API that reflect that change. So you also use given ... will ...style from BDDMockito
如今,测试方法已经演变为行为驱动开发(来自 Dan North),这几乎是同一件事,但更多地关注设计时的行为。为了反映这种想法,人们要求 Mockito 提供反映这种变化的 API。所以你也使用given ... will ...style fromBDDMockito
given(the_type.performs_that()).willReturn(something)
This is my preferred vocabulary now as I use tests to drive my objects design.
这是我现在首选的词汇,因为我使用测试来驱动我的对象设计。