mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add public field support
This commit is contained in:
@@ -3,7 +3,6 @@ package cn.hutool.core.bean;
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.bean.copier.ValueProvider;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -101,6 +100,20 @@ public class BeanUtilTest {
|
||||
Assert.assertEquals(12, person.getAge());
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试public类型的字段注入是否成功
|
||||
*/
|
||||
@Test
|
||||
public void mapToBeanTest2() {
|
||||
HashMap<String, Object> map = CollUtil.newHashMap();
|
||||
map.put("name", "Joe");
|
||||
map.put("age", 12);
|
||||
|
||||
Person2 person = BeanUtil.mapToBean(map, Person2.class, CopyOptions.create());
|
||||
Assert.assertEquals("Joe", person.name);
|
||||
Assert.assertEquals(12, person.age);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanToMapTest() {
|
||||
SubPerson person = new SubPerson();
|
||||
@@ -264,4 +277,10 @@ public class BeanUtilTest {
|
||||
private int age;
|
||||
private String openid;
|
||||
}
|
||||
|
||||
public static class Person2 {
|
||||
public String name;
|
||||
public int age;
|
||||
public String openid;
|
||||
}
|
||||
}
|
||||
|
@@ -80,4 +80,11 @@ public class ConvertToBeanTest {
|
||||
Assert.assertEquals("测试A11", subPerson.getName());
|
||||
Assert.assertEquals("11213232", subPerson.getOpenid());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullStrToBeanTest(){
|
||||
String nullStr = "null";
|
||||
final SubPerson subPerson = Convert.convert(SubPerson.class, nullStr);
|
||||
Assert.assertNull(subPerson);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user