优化 PreconditionsExt 测试代码

dev
ZhouXY108 2024-08-30 14:56:38 +08:00
parent 5c1f6046ed
commit 0b9635880e
2 changed files with 18 additions and 14 deletions

View File

@ -19,6 +19,8 @@ package xyz.zhouxy.plusone.commons.util;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Supplier; import java.util.function.Supplier;
import javax.annotation.Nonnull;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
/** /**
@ -30,7 +32,7 @@ import com.google.common.base.Preconditions;
*/ */
public class PreconditionsExt { public class PreconditionsExt {
public static <E extends Throwable> void check(boolean condition, Supplier<E> e) throws E { public static <E extends Throwable> void check(boolean condition, @Nonnull Supplier<E> e) throws E {
if (!condition) { if (!condition) {
throw e.get(); throw e.get();
} }

View File

@ -1,9 +1,9 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import xyz.zhouxy.plusone.commons.exception.BaseException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -22,33 +22,35 @@ class PreconditionsExtTests {
Object[] array = null; Object[] array = null;
PreconditionsExt.checkAllNotNull(array); PreconditionsExt.checkAllNotNull(array);
}); });
Object obj = new Object();
assertNotNull(obj);
assertThrows(NullPointerException.class, assertThrows(NullPointerException.class,
() -> PreconditionsExt.checkAllNotNull(new Object[]{new Object(), null})); () -> PreconditionsExt.checkAllNotNull(new Object[]{obj, null}));
assertThrows(NullPointerException.class, assertThrows(NullPointerException.class,
() -> PreconditionsExt.checkAllNotNull(new Object(), null)); () -> PreconditionsExt.checkAllNotNull(obj, null));
List<Object> list = Arrays.asList(obj, null);
assertNotNull(list);
assertThrows(NullPointerException.class, assertThrows(NullPointerException.class,
() -> PreconditionsExt.checkAllNotNull(Arrays.asList(new Object(), null))); () -> PreconditionsExt.checkAllNotNull(list));
PreconditionsExt.checkAllNotNull(new Object[]{new Object(), "Test"}); PreconditionsExt.checkAllNotNull(new Object[]{obj, "Test"});
PreconditionsExt.checkAllNotNull(new Object(), "Test"); PreconditionsExt.checkAllNotNull(obj, "Test");
PreconditionsExt.checkAllNotNull(Arrays.asList(new Object(), "Test")); PreconditionsExt.checkAllNotNull(Arrays.asList(obj, "Test"));
} }
} }
class TestException extends BaseException { class TestException extends Exception {
private static final long serialVersionUID = -8808661764734834820L; private static final long serialVersionUID = -8808661764734834820L;
private static final String ERR_CODE = "TEST";
protected TestException(String msg) { protected TestException(String msg) {
super(ERR_CODE, msg); super(msg);
} }
protected TestException(Throwable cause) { protected TestException(Throwable cause) {
super(ERR_CODE, cause); super(cause);
} }
protected TestException(String msg, Throwable cause) { protected TestException(String msg, Throwable cause) {
super(ERR_CODE, msg, cause); super(msg, cause);
} }
} }