forked from plusone/plusone-commons
test: 测试工具类的私有构造器
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
* Copyright 2024-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,9 +17,16 @@
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public
|
||||
class NumbersTests {
|
||||
@@ -184,4 +191,18 @@ class NumbersTests {
|
||||
BigDecimal result = Numbers.nullToZero(value);
|
||||
assertEquals(BigDecimal.ZERO, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
|
||||
Constructor<?>[] constructors = Numbers.class.getDeclaredConstructors();
|
||||
Arrays.stream(constructors)
|
||||
.forEach(constructor -> {
|
||||
assertFalse(constructor.isAccessible());
|
||||
constructor.setAccessible(true);
|
||||
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
|
||||
.getCause();
|
||||
assertInstanceOf(IllegalStateException.class, cause);
|
||||
assertEquals("Utility class", cause.getMessage());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user