diff --git a/patterns-modules/design-patterns-behavioral-2/src/main/java/com/baeldung/nullconversion/Delivery.java b/patterns-modules/design-patterns-behavioral-2/src/main/java/com/baeldung/nullconversion/Delivery.java index 3c584d9566..7f18cfd9bc 100644 --- a/patterns-modules/design-patterns-behavioral-2/src/main/java/com/baeldung/nullconversion/Delivery.java +++ b/patterns-modules/design-patterns-behavioral-2/src/main/java/com/baeldung/nullconversion/Delivery.java @@ -32,4 +32,7 @@ public class Delivery { public static Delivery freeDelivery() { return new Delivery("Free delivery"); } + public static Delivery defaultDelivery() { + return new Delivery("Default delivery"); + } } diff --git a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/OnePersonDeliveryServiceUnitTest.java b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/OnePersonDeliveryServiceUnitTest.java index 01cd658bd1..304458b213 100644 --- a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/OnePersonDeliveryServiceUnitTest.java +++ b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/OnePersonDeliveryServiceUnitTest.java @@ -7,6 +7,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import com.baeldung.nullconversion.service.OnePersonExplicitDeliveryService; import com.baeldung.nullconversion.service.OnePersonGuavaOptionalDeliveryService; import com.baeldung.nullconversion.service.OnePersonOptionalDeliveryService; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ArgumentsSource; @@ -27,13 +28,4 @@ class OnePersonDeliveryServiceUnitTest { Delivery actual = deliveryService.calculateDeliveryForPerson(1L); assertThat(actual).isEqualTo(expected); } - - @ParameterizedTest - @ArgumentsSource(NullReturningPersonChainProvider.class) - void givenMockDeliverServiceWhenNullValuesThenGuavaOptionalServiceThrowsException(Person person) { - DeliveryService deliveryService = new OnePersonGuavaOptionalDeliveryService(person); - assertThatExceptionOfType(NullPointerException.class) - .isThrownBy(() -> deliveryService.calculateDeliveryForPerson(1L)); - - } } \ No newline at end of file diff --git a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/PersonProvider.java b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/PersonProvider.java index d31d9d8ec4..301e859fe7 100644 --- a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/PersonProvider.java +++ b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/PersonProvider.java @@ -18,10 +18,10 @@ public class PersonProvider implements ArgumentsProvider { return Stream.of( Arguments.of(person, Delivery.freeDelivery()), - Arguments.of(cloneAndMutate(person, p -> p.getAddress().getZipCode().setCode("")), null), - Arguments.of(cloneAndMutate(person, p -> p.getAddress().setZipCode(null)), null), - Arguments.of(cloneAndMutate(person, p -> p.setAddress(null)), null), - Arguments.of(null, null) + Arguments.of(cloneAndMutate(person, p -> p.getAddress().getZipCode().setCode("")), Delivery.defaultDelivery()), + Arguments.of(cloneAndMutate(person, p -> p.getAddress().setZipCode(null)), Delivery.defaultDelivery()), + Arguments.of(cloneAndMutate(person, p -> p.setAddress(null)), Delivery.defaultDelivery()), + Arguments.of(null, Delivery.defaultDelivery()) ); } diff --git a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/MockOnePersonDeliveryServiceBase.java b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/MockOnePersonDeliveryServiceBase.java index 973f8bce97..1080f69d97 100644 --- a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/MockOnePersonDeliveryServiceBase.java +++ b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/MockOnePersonDeliveryServiceBase.java @@ -23,7 +23,7 @@ public abstract class MockOnePersonDeliveryServiceBase implements DeliveryServic String code = zipCode.getCode(); return calculateDeliveryForZipCode(code); } - return null; + return Delivery.defaultDelivery(); } public Delivery calculateDeliveryForPersonWithOptional(Long id) { @@ -32,7 +32,7 @@ public abstract class MockOnePersonDeliveryServiceBase implements DeliveryServic .map(Address::getZipCode) .map(ZipCode::getCode) .map(this::calculateDeliveryForZipCode) - .orElse(null); + .orElse(Delivery.defaultDelivery()); } protected Person getPersonById(Long id) { @@ -41,7 +41,7 @@ public abstract class MockOnePersonDeliveryServiceBase implements DeliveryServic protected Delivery calculateDeliveryForZipCode(String zipCode) { if (zipCode == null || zipCode.isEmpty()) { - return null; + return Delivery.defaultDelivery(); } else { return Delivery.freeDelivery(); } diff --git a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonExplicitDeliveryService.java b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonExplicitDeliveryService.java index 652d0a002b..7d81d3cf51 100644 --- a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonExplicitDeliveryService.java +++ b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonExplicitDeliveryService.java @@ -19,6 +19,6 @@ public class OnePersonExplicitDeliveryService extends MockOnePersonDeliveryServi String code = zipCode.getCode(); return calculateDeliveryForZipCode(code); } - return null; + return Delivery.defaultDelivery(); } } diff --git a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonGuavaOptionalDeliveryService.java b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonGuavaOptionalDeliveryService.java index 29d42789a0..f495576e87 100644 --- a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonGuavaOptionalDeliveryService.java +++ b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonGuavaOptionalDeliveryService.java @@ -20,7 +20,7 @@ public class OnePersonGuavaOptionalDeliveryService extends MockOnePersonDelivery .transform(Address::getZipCode) .transform(ZipCode::getCode) .transform(this::calculateDeliveryForZipCode) - .orNull(); + .or(Delivery.defaultDelivery()); } } diff --git a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonOptionalDeliveryService.java b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonOptionalDeliveryService.java index 586f0009ea..1a7ab5d870 100644 --- a/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonOptionalDeliveryService.java +++ b/patterns-modules/design-patterns-behavioral-2/src/test/java/com/baeldung/nullconversion/service/OnePersonOptionalDeliveryService.java @@ -20,7 +20,7 @@ public class OnePersonOptionalDeliveryService extends MockOnePersonDeliveryServi .map(Address::getZipCode) .map(ZipCode::getCode) .map(this::calculateDeliveryForZipCode) - .orElse(null); + .orElse(Delivery.defaultDelivery()); } }