User Tools

Site Tools


mockito

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
mockito [2019/07/17 16:05] – [BDDMockito] kalaschnikowmockito [2020/03/03 10:48] (current) – [Argument Matchers] kalaschnikow
Line 1: Line 1:
 +====== Mockito ======
 +
 +[[https://site.mockito.org/|Official site]]
 +
 +
 +
 +===== Mocks =====
 +
 +===== Spies =====
 +
 +===== Argument Matchers =====
 +  * we can configure our mocked methods with argument matchers
 +<code java>
 +when(flowerService.analyze(anyString())).thenReturn("Flower");
 +</code>
 +
 +  * **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://static.javadoc.io/org.mockito/mockito-core/3.0.0/org/mockito/BDDMockito.html|BDDMockito]] - JavaDoc
 +  * https://riptutorial.com/mockito/topic/2055/getting-started-with-mockito
 +
 +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, containBread());
 +}
 +</code>
 +
 +