java读取yaml文件
yaml语法
详见:yaml语法
应用场景
自动化测试中,用例数据存放在yaml文件中
数据
person.yaml
--- id: 1 name: 韧 age: 18 --- id: 2 name: qzcsbj age: 19
person2.yaml
--- - id: 3 name: 小韧 age: 18 - id: 4 name: 全栈测试笔记 age: 19
person3.yaml
--- - id: 5 name: 韧哥 age: 18 - id: 6 name: 性能测试 age: 19
Person.java
package com.test.bean; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Person { private String id; private String name; private String age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "Person{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
读取并返回对象
package com.test.utils; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.test.bean.Person; import java.io.IOException; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class ReadDataFromYaml { public static Person getData(String path, Person p){ ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); try { return objectMapper.readValue(ReadDataFromYaml.class.getResourceAsStream(path), p.getClass()); } catch (IOException e) { System.out.println("读取异常:" + e); } return null; } public static void main(String[] args) { Person p = new Person(); p = getData("/caseData/person.yaml", p); System.out.println(p); if (p!=null){ System.out.println(p.getId()); System.out.println(p.getName()); System.out.println(p.getAge()); } } }
结果
优化:泛型,传字节码
package com.test.utils; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.test.bean.Person; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class ReadDataFromYaml { public static <T> T getData(String path, Class<T> clazz){ ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); try { T obj = objectMapper.readValue(ReadDataFromYaml.class.getResourceAsStream(path), clazz); return obj; } catch (Exception e) { System.out.println("异常:" + e); } return null; } public static void main(String[] args) { Person p = getData("/caseData/person.yaml", Person.class); System.out.println(p); if (p!=null){ System.out.println(p.getId()); System.out.println(p.getName()); System.out.println(p.getAge()); } } }
结果
读取并返回对象list
package com.test.utils; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.test.bean.Person; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class ReadDataFromYaml { public static List<Person> getData(String path, List<Person> plist){ ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); try { plist = objectMapper.readValue(ReadDataFromYaml.class.getResourceAsStream(path), plist.getClass()); return plist; } catch (IOException e) { System.out.println("读取异常:" + e); } return null; } public static void main(String[] args) { List<Person> plist = new ArrayList<Person>(); plist = getData("/caseData/person2.yaml", plist); System.out.println(plist); } }
结果
优化:泛型,传字节码
package com.test.utils; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.test.bean.Person; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class ReadDataFromYaml { public static <T> List<T> getData(String path, Class<T> clazz){ ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); try { List<T> list = objectMapper.readValue(ReadDataFromYaml.class.getResourceAsStream(path), List.class); return list; } catch (IOException e) { System.out.println("读取异常:" + e); } return null; } public static void main(String[] args) { List<Person> plist = new ArrayList<Person>(); List<Person> plist1 = getData("/caseData/person2.yaml", Person.class); if ( plist1!=null){ plist.addAll(plist1); } List<Person> plist2 = getData("/caseData/person2.yaml", Person.class); if ( plist2!=null){ plist.addAll(plist2); } System.out.println(plist); } }
结果
问题
上面可以把多个yaml的数据放到一个list里面
但是,如果打印对象内容(模拟解析测试用例数据)
public static void main(String[] args) { List<Person> plist = new ArrayList<Person>(); List<Person> plist1 = getData("/caseData/person2.yaml", Person.class); if ( plist1!=null){ plist.addAll(plist1); } List<Person> plist2 = getData("/caseData/person2.yaml", Person.class); if ( plist2!=null){ plist.addAll(plist2); } System.out.println(plist); for (Person person : plist2) { System.out.println(person.getName()); } }
结果报错:
[{id=3, name=小韧, age=18}, {id=4, name=全栈测试笔记, age=19}, {id=3, name=小韧, age=18}, {id=4, name=全栈测试笔记, age=19}] Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.test.bean.Person at com.test.utils.ReadDataFromYaml.main(ReadDataFromYaml.java:39)
debug发现,list的元素是LinkedHashMap类型
解决方案:转换为指定类型
package com.test.utils; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.test.bean.Person; import java.util.ArrayList; import java.util.List; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class ReadDataFromYaml { public static List<Person> getData(String path) { ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); try { List list = objectMapper.readValue(ReadDataFromYaml.class.getResourceAsStream(path), List.class); // 转成指定类型的list集合 List<Person> list_new = objectMapper.convertValue(list, new TypeReference<List<Person>>() {}); return list_new; } catch (Exception e) { System.out.println("异常:" + e); } return null; } public static void main(String[] args) { List<Person> plist = new ArrayList<Person>(); List<Person> plist1 = getData("/caseData/person2.yaml"); if ( plist1!=null){ plist.addAll(plist1); } List<Person> plist2 = getData("/caseData/person3.yaml"); if ( plist2!=null){ plist.addAll(plist2); for (Person person : plist2) { System.out.println(person.getId()); System.out.println(person.getName()); System.out.println(person.getAge()); } } System.out.println(plist); } }
结果
思考题
上面解决方案中,类型写死了,如果作为一个工具类,如何实现传任意类型?
__EOF__
![微信:ren168632201](https://files-cdn.cnblogs.com/files/uncleyong/wx.bmp)
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!