Preconditions
Preconditions
- 没有参数,抛出的异常没有错误信息。
- 一个额外的
Object 参数作为错误信息。异常会被抛出一个错误信息。 - 一个额外的字符串参数,以及任意数量的额外对象参数作为一个占位符的错误信息。它的行为有点像
printf ,但为了GWT 的兼容性和效率,它只允许使用%s 指示器
我们来看看如何使用
checkArgument
@Test
public void whenCheckArgumentEvaluatesFalse_throwsException() {
int age = -18;
assertThatThrownBy(() -> Preconditions.checkArgument(age > 0))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(null).hasNoCause();
}
我们可以通过传递错误信息从
@Test
public void givenErrorMsg_whenCheckArgEvalsFalse_throwsException() {
int age = -18;
String message = "Age can't be zero or less than zero.";
assertThatThrownBy(() -> Preconditions.checkArgument(age > 0, message))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(message).hasNoCause();
}
我们可以通过传递一个错误信息,从
@Test
public void givenTemplateMsg_whenCheckArgEvalsFalse_throwsException() {
int age = -18;
String message = "Age should be positive number, you supplied %s.";
assertThatThrownBy(
() -> Preconditions.checkArgument(age > 0, message, age))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(message, age).hasNoCause();
}
checkElementIndex
方法
让我们看看如何使用这个方法,通过在检查
@Test
public void givenArrayAndMsg_whenCheckElementEvalsFalse_throwsException() {
int[] numbers = { 1, 2, 3, 4, 5 };
String message = "Please check the bound of an array and retry";
assertThatThrownBy(() ->
Preconditions.checkElementIndex(6, numbers.length - 1, message))
.isInstanceOf(IndexOutOfBoundsException.class)
.hasMessageStartingWith(message).hasNoCause();
}
checkNotNull
方法
@Test
public void givenNullString_whenCheckNotNullWithMessage_throwsException () {
String nullObject = null;
String message = "Please check the Object supplied, its null!";
assertThatThrownBy(() -> Preconditions.checkNotNull(nullObject, message))
.isInstanceOf(NullPointerException.class)
.hasMessage(message).hasNoCause();
}
我们还可以通过向错误信息传递一个参数,从
@Test
public void whenCheckNotNullWithTemplateMessage_throwsException() {
String nullObject = null;
String message = "Please check the Object supplied, its %s!";
assertThatThrownBy(
() -> Preconditions.checkNotNull(nullObject, message,
new Object[] { null }))
.isInstanceOf(NullPointerException.class)
.hasMessage(message, nullObject).hasNoCause();
}
checkPositionIndex
方法
如果传递的索引不在
@Test
public void givenArrayAndMsg_whenCheckPositionEvalsFalse_throwsException() {
int[] numbers = { 1, 2, 3, 4, 5 };
String message = "Please check the bound of an array and retry";
assertThatThrownBy(
() -> Preconditions.checkPositionIndex(6, numbers.length - 1, message))
.isInstanceOf(IndexOutOfBoundsException.class)
.hasMessageStartingWith(message).hasNoCause();
}
checkState
方法
让我们看看如何使用这个方法,通过在
@Test
public void givenStatesAndMsg_whenCheckStateEvalsFalse_throwsException() {
int[] validStates = { -1, 0, 1 };
int givenState = 10;
String message = "You have entered an invalid state";
assertThatThrownBy(
() -> Preconditions.checkState(
Arrays.binarySearch(validStates, givenState) > 0, message))
.isInstanceOf(IllegalStateException.class)
.hasMessageStartingWith(message).hasNoCause();
}