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:
		
							parent
							
								
									6067f9cc3c
								
							
						
					
					
						commit
						b4e12c3cbf
					
				| @ -0,0 +1,102 @@ | ||||
| package com.baeldung.mockito.fluentapi; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class Pizza { | ||||
| 
 | ||||
|     public enum PizzaSize { | ||||
|         LARGE, MEDIUM, SMALL; | ||||
|     } | ||||
| 
 | ||||
|     private String name; | ||||
|     private PizzaSize size; | ||||
|     private List<String> toppings; | ||||
|     private boolean stuffedCrust; | ||||
|     private boolean collect; | ||||
|     private Integer discount; | ||||
| 
 | ||||
|     private Pizza(PizzaBuilder builder) { | ||||
|         this.name = builder.name; | ||||
|         this.size = builder.size; | ||||
|         this.toppings = builder.toppings; | ||||
|         this.stuffedCrust = builder.stuffedCrust; | ||||
|         this.collect = builder.collect; | ||||
|         this.discount = builder.discount; | ||||
|     } | ||||
| 
 | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
| 
 | ||||
|     public PizzaSize getSize() { | ||||
|         return size; | ||||
|     } | ||||
| 
 | ||||
|     public List<String> getToppings() { | ||||
|         return toppings; | ||||
|     } | ||||
| 
 | ||||
|     public boolean isStuffedCrust() { | ||||
|         return stuffedCrust; | ||||
|     } | ||||
| 
 | ||||
|     public boolean isCollecting() { | ||||
|         return collect; | ||||
|     } | ||||
| 
 | ||||
|     public Integer getDiscount() { | ||||
|         return discount; | ||||
|     } | ||||
| 
 | ||||
|     public static class PizzaBuilder { | ||||
|         private String name; | ||||
|         private PizzaSize size; | ||||
| 
 | ||||
|         private List<String> toppings; | ||||
|         private boolean stuffedCrust; | ||||
|         private boolean collect; | ||||
|         private Integer discount = null; | ||||
| 
 | ||||
|         public PizzaBuilder() { | ||||
|         } | ||||
| 
 | ||||
|         public PizzaBuilder name(String name) { | ||||
|             this.name = name; | ||||
|             return this; | ||||
|         } | ||||
| 
 | ||||
|         public PizzaBuilder size(PizzaSize size) { | ||||
|             this.size = size; | ||||
|             return this; | ||||
|         } | ||||
| 
 | ||||
|         public PizzaBuilder withExtraTopping(String extraTopping) { | ||||
|             if (this.toppings == null) { | ||||
|                 toppings = new ArrayList<>(); | ||||
|             } | ||||
|             this.toppings.add(extraTopping); | ||||
|             return this; | ||||
|         } | ||||
| 
 | ||||
|         public PizzaBuilder withStuffedCrust(boolean stuffedCrust) { | ||||
|             this.stuffedCrust = stuffedCrust; | ||||
|             return this; | ||||
|         } | ||||
| 
 | ||||
|         public PizzaBuilder willCollect(boolean collect) { | ||||
|             this.collect = collect; | ||||
|             return this; | ||||
|         } | ||||
| 
 | ||||
|         public PizzaBuilder applyDiscount(Integer discount) { | ||||
|             this.discount = discount; | ||||
|             return this; | ||||
|         } | ||||
| 
 | ||||
|         public Pizza build() { | ||||
|             return new Pizza(this); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,23 @@ | ||||
| package com.baeldung.mockito.fluentapi; | ||||
| 
 | ||||
| import com.baeldung.mockito.fluentapi.Pizza.PizzaSize; | ||||
| 
 | ||||
| public class PizzaService { | ||||
| 
 | ||||
|     private Pizza.PizzaBuilder builder; | ||||
| 
 | ||||
|     public PizzaService(Pizza.PizzaBuilder builder) { | ||||
|         this.builder = builder; | ||||
|     } | ||||
| 
 | ||||
|     public Pizza orderHouseSpecial() { | ||||
|         return builder.name("Special") | ||||
|             .size(PizzaSize.LARGE) | ||||
|             .withExtraTopping("Mushrooms") | ||||
|             .withStuffedCrust(true) | ||||
|             .withExtraTopping("Chilli") | ||||
|             .willCollect(true) | ||||
|             .applyDiscount(20) | ||||
|             .build(); | ||||
|     } | ||||
| } | ||||
| @ -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()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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()); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user