mockito
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| mockito [2019/07/17 15:26] – [BDDMockito] kalaschnikow | mockito [2020/03/03 10:48] (current) – [Argument Matchers] kalaschnikow | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Mockito ====== | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | |||
| + | |||
| + | ===== Mocks ===== | ||
| + | |||
| + | ===== Spies ===== | ||
| + | |||
| + | ===== Argument Matchers ===== | ||
| + | * we can configure our mocked methods with argument matchers | ||
| + | <code java> | ||
| + | when(flowerService.analyze(anyString())).thenReturn(" | ||
| + | </ | ||
| + | |||
| + | * **We can't use argument matchers as a return value**, an exact value is required when stubbing calls | ||
| + | * we can't use argument matchers outside of verification or stubbing | ||
| + | ===== BDDMockito ===== | ||
| + | |||
| + | Links: | ||
| + | * [[https:// | ||
| + | * https:// | ||
| + | |||
| + | BDD-phases: | ||
| + | * given | ||
| + | * when | ||
| + | * then | ||
| + | |||
| + | |||
| + | <code java> | ||
| + | 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, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
