staging changes
This commit is contained in:
parent
e42ce73616
commit
ad17d70bbb
@ -1,5 +1,6 @@
|
||||
package com.baeldung.mockito.fluentapi;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Pizza {
|
||||
@ -11,23 +12,51 @@ public class Pizza {
|
||||
private String name;
|
||||
private PizzaSize size;
|
||||
private List<String> toppings;
|
||||
|
||||
private String email;
|
||||
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 String email;
|
||||
|
||||
private List<String> toppings = new ArrayList<>();
|
||||
private boolean stuffedCrust;
|
||||
private List<String> toppings;
|
||||
private boolean collect;
|
||||
private Integer discount = null;
|
||||
|
||||
public PizzaBuilder(String name) {
|
||||
this.name = name;
|
||||
@ -48,13 +77,13 @@ public class Pizza {
|
||||
return this;
|
||||
}
|
||||
|
||||
public BankAccountBuilder willCollect(boolean collect) {
|
||||
this.newsletter = newsletter;
|
||||
public PizzaBuilder willCollect(boolean collect) {
|
||||
this.collect = collect;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BankAccountBuilder applyDiscount(boolean collect) {
|
||||
this.newsletter = newsletter;
|
||||
public PizzaBuilder applyDiscount(Integer discount) {
|
||||
this.discount = discount;
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -63,20 +92,4 @@ public class Pizza {
|
||||
}
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public PizzaSize getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public boolean isNewsletter() {
|
||||
return newsletter;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,29 @@
|
||||
package com.baeldung.mockito.fluentapi;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.baeldung.mockito.fluentapi.Pizza.PizzaSize;
|
||||
|
||||
public class PizzaService {
|
||||
|
||||
private Pizza pizza;
|
||||
|
||||
public PizzaService(Pizza.PizzaBuilder builder) {
|
||||
this.pizza = builder.build();
|
||||
}
|
||||
|
||||
public List<String> listToppings(Pizza.PizzaBuilder builder) {
|
||||
Pizza build = builder.size(PizzaSize.LARGE)
|
||||
.withExtaTopping("Mushroom")
|
||||
.withStuffedCrust(false)
|
||||
.willCollect(true)
|
||||
.applyDiscount(20)
|
||||
.build();
|
||||
}
|
||||
|
||||
public boolean isLarge() {
|
||||
return PizzaSize.LARGE.equals(pizza.getSize());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,23 +1,86 @@
|
||||
package com.baeldung.mockito.fluentapi;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.baeldung.mockito.fluentapi.Pizza.PizzaSize;
|
||||
|
||||
public class PizzaServiceUnitTest {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private ArrayList<String> mockList;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
public void givenPizzaWithToppings_whenServiceInvoked_thenReturnsList() {
|
||||
|
||||
//Foo mock = mock(Foo.class, RETURNS_DEEP_STUBS);
|
||||
|
||||
List<String> bob = Arrays.asList("a1", "a2", "b1", "c2", "c1");
|
||||
|
||||
|
||||
when(mockList.stream()
|
||||
.filter(s -> s.startsWith("c"))
|
||||
.map(String::toUpperCase)
|
||||
.sorted()
|
||||
.collect(Collectors.toList()))
|
||||
.thenReturn(bob);
|
||||
|
||||
Assert.assertEquals(bob, mockList.stream()
|
||||
.filter(s -> s.startsWith("c"))
|
||||
.map(String::toUpperCase)
|
||||
.sorted()
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
|
||||
// List<String> filtered = Arrays.asList("a1", "a2", "b1", "c2", "c1")
|
||||
// .stream()
|
||||
// .filter(s -> s.startsWith("c"))
|
||||
// .map(String::toUpperCase)
|
||||
// .sorted()
|
||||
// .collect(Collectors.toList());
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// Pizza.PizzaBuilder builder = Mockito.mock(Pizza.PizzaBuilder.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
//
|
||||
// Pizza pizza = Mockito.mock(Pizza.class);
|
||||
//
|
||||
// Mockito.when(builder
|
||||
// .size(PizzaSize.LARGE)
|
||||
// .withExtaTopping("Mushroom")
|
||||
// .withStuffedCrust(false)
|
||||
// .willCollect(true)
|
||||
// .applyDiscount(20)
|
||||
// .build()).thenReturn(pizza);
|
||||
//
|
||||
// PizzaService service = new PizzaService(builder);
|
||||
//
|
||||
// List<String> listToppings = service.listToppings();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public List<String> convertAllToUpperCase(List<String> words) {
|
||||
|
||||
return words.stream()
|
||||
.map(String::toUpperCase)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,43 @@
|
||||
package com.baeldung.mockito.fluentapi;
|
||||
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.mockito.fluentapi.Pizza.PizzaSize;
|
||||
|
||||
public class PizzaUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenPizza_whenBuilt_thenShouldReturnPizzaWithCorrectAttributes() {
|
||||
Pizza pizza = new Pizza
|
||||
.PizzaBuilder("Margherita")
|
||||
.size(PizzaSize.LARGE)
|
||||
.withExtaTopping("Mushroom")
|
||||
.withStuffedCrust(false)
|
||||
.willCollect(true)
|
||||
.applyDiscount(20)
|
||||
.build();
|
||||
|
||||
//assert
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPizza_whenBuiltWithTraditonalMock_thenShouldReturnPizza() {
|
||||
|
||||
Mock
|
||||
|
||||
|
||||
Pizza pizza = new Pizza
|
||||
.PizzaBuilder("Margherita")
|
||||
.size(PizzaSize.LARGE)
|
||||
.withExtaTopping("Mushroom")
|
||||
.withStuffedCrust(false)
|
||||
.willCollect(true)
|
||||
.applyDiscount(20)
|
||||
.build();
|
||||
|
||||
//assert
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user