Skip to content

Instantly share code, notes, and snippets.

@oleg
Created February 27, 2012 18:46
Show Gist options
  • Save oleg/1926142 to your computer and use it in GitHub Desktop.
Save oleg/1926142 to your computer and use it in GitHub Desktop.
Mockito BDD style
package logic;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.matchers.JUnitMatchers.hasItem;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;
public class BddTest extends Assert {
Seller seller;
Shop shop;
@Before
public void setUp() throws Exception {
seller = mock(Seller.class);
shop = new Shop(seller);
}
@Test
public void shouldBuyBread() throws Exception {
//given
given(seller.askForBread()).willReturn(new Bread());
//when
List<Object> goods = shop.buyBread();
//then
assertThat(goods, hasItem(is(Bread.class)));
}
private static class Seller {
public Bread askForBread() {
return null;
}
}
private static class Shop {
public Shop(Seller seller) {
}
public List<Object> buyBread() {
return null;
}
}
private static class Bread {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment