Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2007 Mockito contributors
   * This program is made available under the terms of the MIT License.
   */
  package org.mockito;
  
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 given(java.lang.Object) method. Now it really nicely integrates with the given component of a BDD style test!

Here is how the test might look like:

 import static org.mockito.BDDMockito.*;
 
 Seller seller = mock(Seller.class);
 Shop shop = new Shop(seller);
 
 public void shouldBuyBread() throws Exception {
   //given  
   given(seller.askForBread()).willReturn(new Bread());
   
   //when
   Goods goods = shop.buyBread();
   
   //then
   assertThat(goods, containBread());
 }  
 
Stubbing voids with throwables:
   //given
   willThrow(new RuntimeException("boo")).given(mock).foo();
   
   //when
   Result result = systemUnderTest.perform();
   
   //then
   assertEquals(failure, result);
 

One of the purposes of BDDMockito is also to show how to tailor the mocking syntax to a different programming style.

 
 @SuppressWarnings("unchecked")
 public class BDDMockito extends Mockito {
    
    
 
     public static interface BDDMyOngoingStubbing<T> {
        
        
 
         BDDMyOngoingStubbing<T> willAnswer(Answer<?> answer);
        
        
 
         BDDMyOngoingStubbing<T> willReturn(T value);
        
        
 
         BDDMyOngoingStubbing<T> willReturn(T value, T... values);
        
        
 
         BDDMyOngoingStubbing<T> willThrow(Throwable... throwables);

        
 
     }
     
     public static class BDDOngoingStubbingImpl<T> implements BDDMyOngoingStubbing<T> {
 
         private final OngoingStubbing<T> mockitoOngoingStubbing;
 
         public BDDOngoingStubbingImpl(OngoingStubbing<T> ongoingStubbing) {
             this. = ongoingStubbing;
         }
 
         /* (non-Javadoc)
          * @see org.mockitousage.customization.BDDMockito.BDDMyOngoingStubbing#willAnswer(org.mockito.stubbing.Answer)
          */
        public BDDMyOngoingStubbing<T> willAnswer(Answer<?> answer) {
            return new BDDOngoingStubbingImpl<T>(.thenAnswer(answer));
        }
        /* (non-Javadoc)
         * @see org.mockitousage.customization.BDDMockito.BDDMyOngoingStubbing#willReturn(java.lang.Object)
         */
        public BDDMyOngoingStubbing<T> willReturn(T value) {
            return new BDDOngoingStubbingImpl<T>(.thenReturn(value));
        }
        /* (non-Javadoc)
         * @see org.mockitousage.customization.BDDMockito.BDDMyOngoingStubbing#willReturn(java.lang.Object, T[])
         */
        public BDDMyOngoingStubbing<T> willReturn(T value, T... values) {
            return new BDDOngoingStubbingImpl<T>(.thenReturn(valuevalues));
        }
        /* (non-Javadoc)
         * @see org.mockitousage.customization.BDDMockito.BDDMyOngoingStubbing#willThrow(java.lang.Throwable[])
         */
        public BDDMyOngoingStubbing<T> willThrow(Throwable... throwables) {
            return new BDDOngoingStubbingImpl<T>(.thenThrow(throwables));
        }
        public BDDMyOngoingStubbing<T> willCallRealMethod() {
            return new BDDOngoingStubbingImpl<T>(.thenCallRealMethod());
        }
    }
    
    
    public static <T> BDDMyOngoingStubbing<T> given(T methodCall) {
        return new BDDOngoingStubbingImpl<T>(Mockito.when(methodCall));
    }
    
    
    public static interface BDDStubber {
        
        BDDStubber willAnswer(Answer answer);
        
        
        BDDStubber willNothing();
        
        
        BDDStubber willReturn(Object toBeReturned);
        
        
        BDDStubber willThrow(Throwable toBeThrown);
        
        
        <T> T given(T mock);
    }
    
    public static class BDDStubberImpl implements BDDStubber {
        private final Stubber mockitoStubber;
        public BDDStubberImpl(Stubber mockitoStubber) {
            this. = mockitoStubber;
        }
        /* (non-Javadoc)
         * @see org.mockitousage.customization.BDDMockito.BDDStubber#given(java.lang.Object)
         */
        public <T> T given(T mock) {
            return .when(mock);
        }
        /* (non-Javadoc)
         * @see org.mockitousage.customization.BDDMockito.BDDStubber#willAnswer(org.mockito.stubbing.Answer)
         */
        public BDDStubber willAnswer(Answer answer) {
            return new BDDStubberImpl(.doAnswer(answer));
        }
        /* (non-Javadoc)
         * @see org.mockitousage.customization.BDDMockito.BDDStubber#willNothing()
         */
        public BDDStubber willNothing() {
            return new BDDStubberImpl(.doNothing());
        }
        /* (non-Javadoc)
         * @see org.mockitousage.customization.BDDMockito.BDDStubber#willReturn(java.lang.Object)
         */
        public BDDStubber willReturn(Object toBeReturned) {
            return new BDDStubberImpl(.doReturn(toBeReturned));
        }
        /* (non-Javadoc)
         * @see org.mockitousage.customization.BDDMockito.BDDStubber#willThrow(java.lang.Throwable)
         */
        public BDDStubber willThrow(Throwable toBeThrown) {
            return new BDDStubberImpl(.doThrow(toBeThrown));
        }
    }
    
    
    public static BDDStubber willThrow(Throwable toBeThrown) {
        return new BDDStubberImpl(Mockito.doThrow(toBeThrown));
    }
    
    
    public static BDDStubber willAnswer(Answer answer) {
        return new BDDStubberImpl(Mockito.doAnswer(answer));
    }  
    
    
see original Mockito.doNothing()
    public static BDDStubber willDoNothing() {
        return new BDDStubberImpl(Mockito.doNothing());
    }    
    
    
    public static BDDStubber willReturn(Object toBeReturned) {
        return new BDDStubberImpl(Mockito.doReturn(toBeReturned));
    }

    
    public static BDDStubber willCallRealMethod() {
        return new BDDStubberImpl(Mockito.doCallRealMethod());
    }
New to GrepCode? Check out our FAQ X