mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
add toBean support Map
This commit is contained in:
@@ -75,6 +75,22 @@ public class BeanUtilTest {
|
||||
Assert.assertEquals(person.getOpenid(), "DFDFSDFWERWER");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBeanTest(){
|
||||
SubPerson person = new SubPerson();
|
||||
person.setAge(14);
|
||||
person.setOpenid("11213232");
|
||||
person.setName("测试A11");
|
||||
person.setSubName("sub名字");
|
||||
|
||||
final Map<?, ?> map = BeanUtil.toBean(person, Map.class);
|
||||
Assert.assertEquals("测试A11", map.get("name"));
|
||||
Assert.assertEquals(14, map.get("age"));
|
||||
Assert.assertEquals("11213232", map.get("openid"));
|
||||
// static属性应被忽略
|
||||
Assert.assertFalse(map.containsKey("SUBNAME"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapToBeanIgnoreCaseTest() {
|
||||
HashMap<String, Object> map = CollUtil.newHashMap();
|
||||
|
@@ -1,15 +1,15 @@
|
||||
package cn.hutool.core.convert;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtilTest.SubPerson;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtilTest.SubPerson;
|
||||
|
||||
/**
|
||||
* 类型转换工具单元测试<br>
|
||||
* 转换为数组
|
||||
@@ -45,6 +45,12 @@ public class ConvertToBeanTest {
|
||||
Assert.assertEquals("测试A11", map.get("name"));
|
||||
Assert.assertEquals("14", map.get("age"));
|
||||
Assert.assertEquals("11213232", map.get("openid"));
|
||||
|
||||
final LinkedHashMap<String, String> map2 = Convert.convert(
|
||||
new TypeReference<LinkedHashMap<String, String>>() {}, person);
|
||||
Assert.assertEquals("测试A11", map2.get("name"));
|
||||
Assert.assertEquals("14", map2.get("age"));
|
||||
Assert.assertEquals("11213232", map2.get("openid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Reference in New Issue
Block a user