Exercising BDD with Mockito

In BDD, given represents the initial context and when represents the event/condition, but Mockito already has a when style (initial context definition) of method stubbing. Therefore, when doesn't go well with BDD. Thus, the BDDMockito class introduces an alias, so that we can stub method calls with the given(Object) method.

The following JUnit test is implemented in the BDD style:

@RunWith(MockitoJUnitRunner.class)
public class StockBrokerBDDTest {
  @Mock MarketWatcher marketWatcher;
  @Mock Portfolio portfolio;

  StockBroker broker;

  @Before public void setUp() {
    broker = new StockBroker(marketWatcher);
  }

  @Test
  public void should_sell_a_stock_when_price_increases_by_ten_percent(){
    Stock aCorp = new Stock("FB", "FaceBook" ...

Get Mockito Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.