BAEL-3416 - Mockito and Fluent APIs (#8201)

* BAEL-3416 - Mockito and Fluent APIs
- stage changes

* staging changes

* BAEL-3416 - Mockito and Fluent APIs
This commit is contained in:
Jonathan Cook
2019-11-17 17:44:14 +01:00
committed by maibin
parent bb3e595e52
commit dbcf7a1f9d
4 changed files with 245 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
package com.baeldung.mockito.fluentapi;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import com.baeldung.mockito.fluentapi.Pizza.PizzaBuilder;
import com.baeldung.mockito.fluentapi.Pizza.PizzaSize;
public class PizzaServiceUnitTest {
@Mock
private Pizza expectedPizza;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PizzaBuilder anotherbuilder;
@Captor
private ArgumentCaptor<String> stringCaptor;
@Captor
private ArgumentCaptor<Pizza.PizzaSize> sizeCaptor;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void givenTraditonalMocking_whenServiceInvoked_thenPizzaIsBuilt() {
PizzaBuilder nameBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
PizzaBuilder sizeBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
PizzaBuilder firstToppingBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
PizzaBuilder secondToppingBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
PizzaBuilder stuffedBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
PizzaBuilder willCollectBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
PizzaBuilder discountBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
PizzaBuilder builder = Mockito.mock(Pizza.PizzaBuilder.class);
when(builder.name(anyString())).thenReturn(nameBuilder);
when(nameBuilder.size(any(Pizza.PizzaSize.class))).thenReturn(sizeBuilder);
when(sizeBuilder.withExtraTopping(anyString())).thenReturn(firstToppingBuilder);
when(firstToppingBuilder.withStuffedCrust(anyBoolean())).thenReturn(stuffedBuilder);
when(stuffedBuilder.withExtraTopping(anyString())).thenReturn(secondToppingBuilder);
when(secondToppingBuilder.willCollect(anyBoolean())).thenReturn(willCollectBuilder);
when(willCollectBuilder.applyDiscount(anyInt())).thenReturn(discountBuilder);
when(discountBuilder.build()).thenReturn(expectedPizza);
PizzaService service = new PizzaService(builder);
assertEquals("Expected Pizza", expectedPizza, service.orderHouseSpecial());
verify(builder).name(stringCaptor.capture());
assertEquals("Pizza name: ", "Special", stringCaptor.getValue());
verify(nameBuilder).size(sizeCaptor.capture());
assertEquals("Pizza size: ", PizzaSize.LARGE, sizeCaptor.getValue());
}
@Test
public void givenDeepStubs_whenServiceInvoked_thenPizzaIsBuilt() {
Mockito.when(anotherbuilder.name(anyString())
.size(any(Pizza.PizzaSize.class))
.withExtraTopping(anyString())
.withStuffedCrust(anyBoolean())
.withExtraTopping(anyString())
.willCollect(anyBoolean())
.applyDiscount(anyInt())
.build())
.thenReturn(expectedPizza);
PizzaService service = new PizzaService(anotherbuilder);
assertEquals("Expected Pizza", expectedPizza, service.orderHouseSpecial());
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.mockito.fluentapi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.baeldung.mockito.fluentapi.Pizza.PizzaSize;
public class PizzaUnitTest {
@Test
public void givenFluentPizzaApi_whenBuilt_thenPizzaHasCorrectAttributes() {
Pizza pizza = new Pizza.PizzaBuilder()
.name("Margherita")
.size(PizzaSize.LARGE)
.withExtraTopping("Mushroom")
.withStuffedCrust(false)
.willCollect(true)
.applyDiscount(20)
.build();
assertEquals("Pizza name: ", "Margherita", pizza.getName());
assertEquals("Pizza size: ", PizzaSize.LARGE, pizza.getSize());
assertEquals("Extra toppings: ", "Mushroom", pizza.getToppings()
.get(0));
assertFalse("Has stuffed crust: ", pizza.isStuffedCrust());
assertTrue("Will collect: ", pizza.isCollecting());
assertEquals("Discounts: ", Integer.valueOf(20), pizza.getDiscount());
}
}