diff --git a/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java b/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java index 55e3c7db00..aff42ea6e2 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java @@ -3,7 +3,7 @@ package org.baeldung.guava; import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.Arrays; import org.junit.Test; -import static com.google.common.base.Preconditions.*; +import com.google.common.base.*; public class GuavaPreConditionsTest { @@ -11,10 +11,7 @@ public class GuavaPreConditionsTest { public void whenCheckArgumentEvaluatesFalse_throwsException() { int age = -18; - assertThatThrownBy(() -> checkArgument(age > 0)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage(null) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkArgument(age > 0)).isInstanceOf(IllegalArgumentException.class).hasMessage(null).hasNoCause(); } @Test @@ -22,10 +19,7 @@ public class GuavaPreConditionsTest { final int age = -18; final String message = "Age can't be zero or less than zero"; - assertThatThrownBy(() -> checkArgument(age > 0, message)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage(message) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkArgument(age > 0, message)).isInstanceOf(IllegalArgumentException.class).hasMessage(message).hasNoCause(); } @Test @@ -33,19 +27,14 @@ public class GuavaPreConditionsTest { final int age = -18; final String message = "Age can't be zero or less than zero, you supplied %s."; - assertThatThrownBy(() -> checkArgument(age > 0, message, age)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage(message, age) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkArgument(age > 0, message, age)).isInstanceOf(IllegalArgumentException.class).hasMessage(message, age).hasNoCause(); } @Test public void givenArrayOfIntegers_whenCheckElementIndexEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1)) - .isInstanceOf(IndexOutOfBoundsException.class) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkElementIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause(); } @Test @@ -53,20 +42,7 @@ public class GuavaPreConditionsTest { final int[] numbers = { 1, 2, 3, 4, 5 }; final String message = "Please check the bound of an array and retry"; - assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1, message)) - .isInstanceOf(IndexOutOfBoundsException.class) - .hasMessageStartingWith(message) - .hasNoCause(); - } - - @Test - public void givenNullString_whenCheckNotNullCalled_throwsException() { - final String nullObject = null; - - assertThatThrownBy(() -> checkNotNull(nullObject)) - .isInstanceOf(NullPointerException.class) - .hasMessage(null) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkElementIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause(); } @Test @@ -74,10 +50,7 @@ public class GuavaPreConditionsTest { final String nullObject = null; final String message = "Please check the Object supplied, its null!"; - assertThatThrownBy(() -> checkNotNull(nullObject, message)) - .isInstanceOf(NullPointerException.class) - .hasMessage(message) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkNotNull(nullObject, message)).isInstanceOf(NullPointerException.class).hasMessage(message).hasNoCause(); } @Test @@ -85,19 +58,14 @@ public class GuavaPreConditionsTest { final String nullObject = null; final String message = "Please check the Object supplied, its %s!"; - assertThatThrownBy(() -> checkNotNull(nullObject, message, nullObject)) - .isInstanceOf(NullPointerException.class) - .hasMessage(message, nullObject) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkNotNull(nullObject, message, nullObject)).isInstanceOf(NullPointerException.class).hasMessage(message, nullObject).hasNoCause(); } @Test public void givenArrayOfIntegers_whenCheckPositionIndexEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1)) - .isInstanceOf(IndexOutOfBoundsException.class) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkPositionIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause(); } @Test @@ -105,30 +73,14 @@ public class GuavaPreConditionsTest { final int[] numbers = { 1, 2, 3, 4, 5 }; final String message = "Please check the bound of an array and retry"; - assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1, message)) - .isInstanceOf(IndexOutOfBoundsException.class) - .hasMessageStartingWith(message) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkPositionIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause(); } @Test public void givenArrayOfIntegers_whenCheckPositionIndexesEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - assertThatThrownBy(() -> checkPositionIndexes(6, 0, numbers.length - 1)) - .isInstanceOf(IndexOutOfBoundsException.class) - .hasNoCause(); - } - - @Test - public void givenValidStates_whenCheckStateEvaluatesFalse_throwsException() { - final int[] validStates = { -1, 0, 1 }; - final int givenState = 10; - - assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0)) - .isInstanceOf(IllegalStateException.class) - .hasMessage(null) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkPositionIndexes(6, 0, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause(); } @Test @@ -137,10 +89,7 @@ public class GuavaPreConditionsTest { final int givenState = 10; final String message = "You have entered an invalid state"; - assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0, message)) - .isInstanceOf(IllegalStateException.class) - .hasMessageStartingWith(message) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkState(Arrays.binarySearch(validStates, givenState) > 0, message)).isInstanceOf(IllegalStateException.class).hasMessageStartingWith(message).hasNoCause(); } @Test @@ -149,9 +98,7 @@ public class GuavaPreConditionsTest { final int givenState = 10; final String message = "State can't be %s, It can be one of %s."; - assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0, message, givenState, Arrays.toString(validStates))) - .isInstanceOf(IllegalStateException.class) - .hasMessage(message, givenState, Arrays.toString(validStates)) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkState(Arrays.binarySearch(validStates, givenState) > 0, message, givenState, Arrays.toString(validStates))).isInstanceOf(IllegalStateException.class) + .hasMessage(message, givenState, Arrays.toString(validStates)).hasNoCause(); } -} +} \ No newline at end of file