!914 针对cn.hutool.core.convert.Convert的toStr方法,增加了测试覆盖

Merge pull request !914 from happycoder/v6-dev
This commit is contained in:
Looly 2023-01-11 07:09:17 +00:00 committed by Gitee
commit a4bca7715b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -42,6 +42,17 @@ public class ConvertTest {
Assert.assertEquals("aaaa", result); Assert.assertEquals("aaaa", result);
} }
/**
* 调取对象的toString方法会抛异常的测试类
*/
@Data
private static class TestExceptionClass {
@Override
public String toString() {
throw new RuntimeException();
}
}
@Test @Test
public void toStrTest() { public void toStrTest() {
final int a = 1; final int a = 1;
@ -76,6 +87,27 @@ public class ConvertTest {
Assert.assertEquals("640", result); Assert.assertEquals("640", result);
} }
@Test
public void toStrTest5() {
// 被转化的对象有值正常转换
String a = "aaaa";
String aDefaultValue = "aDefault";
String aResult = Convert.toStr(a, aDefaultValue);
Assert.assertEquals(aResult, a);
// 被转化的对象为null返回默认值
String b = null;
String bDefaultValue = "bDefault";
String bResult = Convert.toStr(b, bDefaultValue);
Assert.assertEquals(bResult, bDefaultValue);
// 转换失败返回默认值
TestExceptionClass c = new TestExceptionClass();
String cDefaultValue = "cDefault";
String cResult = Convert.toStr(c, cDefaultValue);
Assert.assertEquals(cResult, cDefaultValue);
}
@Test @Test
public void toIntTest() { public void toIntTest() {
final String a = " 34232"; final String a = " 34232";