首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

mockito 良好mock框架

2014-01-05 
mockito 很好mock框架?package com.zero.orzprofiler.mockitoimport org.junit.Testimport java.util.*

mockito 很好mock框架

?

package com.zero.orzprofiler.mockito;import org.junit.Test;import java.util.*;import static org.mockito.Mockito.*;/** * User: luochao * Date: 14-1-3 * Time: 上午9:54 */public class MockitoTest {    @Test    public  void testVerify() throws RuntimeException{        //mock create        List mockedList = mock(List.class);        mockedList.add("one");        mockedList.clear();        verify(mockedList).add("one");        verify(mockedList).clear();        when(mockedList.get(0)).thenReturn("l234");//        when(mockedList.get(1)).thenThrow(new RuntimeException("runtime exception..."));        System.out.println(mockedList.get(0));        System.out.println(mockedList.get(2));        System.out.println(mockedList.get(1));        List spyList = new LinkedList();        List spy = spy(spyList);        spy.add("one");        when(spy.size()).thenReturn(100);        verify(spy).add("one");        System.out.println(spy.size());        doReturn("lccx").when(spy).get(2);        //verify(mockedList).add("two");        System.out.println(spy.get(2));    }}
?timetunel初始化参数
package com.taobao.timetunnel.tunnel;import static java.lang.Thread.currentThread;import static org.hamcrest.Matchers.is;import static org.junit.Assert.assertThat;import static org.mockito.Mockito.doReturn;import java.nio.ByteBuffer;import java.util.Collections;import java.util.Iterator;import java.util.List;import java.util.concurrent.atomic.AtomicInteger;import org.junit.Before;import org.junit.Test;import org.mockito.Mock;import com.taobao.timetunnel.InjectMocksSupport;import com.taobao.timetunnel.message.Message;import com.taobao.timetunnel.session.Attribute;import com.taobao.timetunnel.session.Session;import com.taobao.timetunnel.tunnel.ByteBufferMessageWatchers;import com.taobao.timetunnel.tunnel.Cursor;import com.taobao.timetunnel.tunnel.Group;import com.taobao.timetunnel.tunnel.Watchable;import com.taobao.timetunnel.tunnel.Watcher;import com.taobao.util.Bytes;/** * @{link WatcherTest} * @author <a href=mailto:jushi@taobao.com>jushi</a> * @created 2010-11-29 *  */public class WatcherTest extends InjectMocksSupport {    @Mock   private Session session;   @Mock   public Message<ByteBuffer> message;   private Watcher<ByteBuffer> watcher;   private static final int TIMES = 10;   private final FakeWatchable watchable = new FakeWatchable();  @Before  public void setUp() throws Exception {    doReturn(3).when(session).intValueOf(Attribute.receiveWindowSize);    doReturn("dw").when(session).stringValueOf(Attribute.subscriber);    doReturn(Bytes.toBuffer(0)).when(message).content();    watcher = new ByteBufferMessageWatchers(watchable).watcher(session);  }@Test  public void shouldAckedGetAndRefluxEqualsPushed() throws Exception {    int ackedGet = 0;    int size = 0;    while (ackedGet + size < TIMES) {      ackedGet += size;      size = watcher.ackAndGet().size();    }    watcher.dispose();    final int pushed = watchable.count.get();    assertThat(watchable.reflux + ackedGet, is(pushed));  }  /**   * @{link FakeWatchable}   */  private final class FakeWatchable implements Watchable<ByteBuffer> {    @Override    public void onAvaliable(final Watcher<ByteBuffer> watcher) {      new Thread(new Runnable() {        @Override        public void run() {          fireOnMessageReceived(watcher);        }      }, "FireOnMessageReceivedTask").start();    }    protected void fireOnMessageReceived(final Watcher<ByteBuffer> watcher) {      System.out.println(currentThread().getName() + currentThread().getId());      for (;;) {        final Iterator<Message<ByteBuffer>> itr = Collections.singleton(message).iterator();        final Cursor<Message<ByteBuffer>> cursor = new Cursor<Message<ByteBuffer>>() {          @Override          public Message<ByteBuffer> next() {            if (itr.hasNext()) return itr.next();            return null;          }        };        watcher.onMessageReceived(cursor);        if (itr.hasNext()) break;        count.incrementAndGet();      }    }    private int reflux;    private final AtomicInteger count = new AtomicInteger();    @Override    public Group<ByteBuffer> groupOf(Watcher<ByteBuffer> watcher) {      return new Group<ByteBuffer>() {        @Override        public void reclaim(Session session, List<Message<ByteBuffer>> reflux) {          FakeWatchable.this.reflux += reflux.size();        }        @Override        public void dispose() {}      };    }    @Override    public void onHasUselessMessages(Watcher<ByteBuffer> watcher) {}}

?

1.?Shorthand for mocks creation -?@Mock?annotationMinimizes repetitive mock creation code.Makes the test class more readable.Makes the verification error easier to read because the?field name?is used to identify the mock.1public?class?ArticleManagerTest {2???3????@Mock?private?ArticleCalculator calculator;4????@Mock?private?ArticleDatabase database;5????@Mock?private?UserProvider userProvider;6???7????private?ArticleManager manager;Important!?This needs to be somewhere in the base class or a test runner:1MockitoAnnotations.initMocks(testClass);when(mock.someMethod("some arg"))02??.thenThrow(new?RuntimeException())03??.thenReturn("foo");04?05//First call: throws runtime exception:06mock.someMethod("some arg");07?08//Second call: prints "foo"09System.out.println(mock.someMethod("some arg"));10?11//Any consecutive call: prints "foo" as well (last stubbing wins).12System.out.println(mock.someMethod("some arg"));

?doReturn()|doThrow()|?doAnswer()|doNothing()|doCallRealMethod()?family of methods?

2.?Spying on real objectsYou can create spies of real objects. When you use the spy then the?real?methods?are called (unless a method was stubbed).

Real spies should be used?carefully and occasionally, for example when dealing with legacy code.

Spying on real objects can be associated with "partial mocking" concept.?Before the release 1.8, Mockito spies were not real partial mocks. The reason was we thought partial mock is a code smell. At some point we found legitimate use cases for partial mocks (3rd party interfaces, interim refactoring of legacy code, the full article is?here)

view sourceprint?01List list =?new?LinkedList();02List spy = spy(list);03?04//optionally, you can stub out some methods:05when(spy.size()).thenReturn(100);06?07//using the spy calls *real* methods08spy.add("one");09spy.add("two");10?11//prints "one" - the first element of a list12System.out.println(spy.get(0));13?14//size() method was stubbed - 100 is printed15System.out.println(spy.size());16?17//optionally, you can verify18verify(spy).add("one");19verify(spy).add("two");//you can create partial mock with spy() method:???2List list = spy(new?LinkedList());3?4//you can enable partial mock capabilities selectively on mocks:5Foo mock = mock(Foo.class);6//Be sure the real implementation is 'safe'.7//If real implementation throws exceptions or depends on specific state of the object then you're in trouble.8when(mock.someMethod()).thenCallRealMethod();


reset(mock);



行为驱动开发

19.?Aliases for behavior driven development?(Since 1.8.0)Behavior Driven Development style of writing tests uses?//given //when //then?comments as fundamental parts of your test methods. This is exactly how we write our tests and we warmly encourage you to do so!

Start learning about BDD here:?http://en.wikipedia.org/wiki/Behavior_Driven_Development

The problem is that current stubbing api with canonical role of?when?word does not integrate nicely with?//given //when //then?comments. It's because stubbing belongs to?given?component of the test and not to the?when?component of the test. Hence?BDDMockito?class introduces an alias so that you stub method calls with?BDDMockito.given(Object)?method. Now it really nicely integrates with the?given?component of a BDD style test!

Here is how the test might look like:

view sourceprint?01import?static?org.mockito.BDDMockito.*;02?03Seller seller = mock(Seller.class);04Shop shop =?new?Shop(seller);05?06public?void?shouldBuyBread()?throws?Exception {07??//given?08??given(seller.askForBread()).willReturn(new?Bread());09???10??//when11??Goods goods = shop.buyBread();12???13??//then14??assertThat(goods, containBread());15}?

?

热点排行