代码
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class Assert {
public static void greaterThan(double num, double exp, String msg) {
if (num < exp) {
throw new IllegalArgumentException(msg);
}
}
public static void notNull(Object object, String msg) {
if (null == object) {
throw new IllegalArgumentException(msg);
}
}
public static void notEmpty(String str, String msg) {
if (null == str || "".equals(str)) {
throw new IllegalArgumentException(msg);
}
}
public static <T> void notEmpty(T[] arr, String msg) {
if (null == arr || arr.length == 0) {
throw new IllegalArgumentException(msg);
}
}
public static <T> T wrap(Callable<T> callable) {
try {
return callable.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void packageNotEmpty(Class<?> clazz,String msg){
if (clazz.getPackage() == null) {
throw new IllegalArgumentException("[" + clazz.getName() + ".java] " + msg);
}
}
}
测试
import org.junit.Test;
public class AssertTest {
@Test(expected = IllegalArgumentException.class)
public void testNotEmpty() {
Assert.notEmpty("", "Not Empty");
Assert.notEmpty(new String[]{}, "Arr Empty");
Assert.notNull(null, "Not null");
}
@Test(expected = RuntimeException.class)
public void testWrap() {
Assert.wrap(() -> {
int a = 1 / 0;
return true;
});
}
@Test
public void testWrap2() {
boolean flag = Assert.wrap(() -> true);
org.junit.Assert.assertEquals(true, flag);
}
}