写点什么

软件测试学习笔记丨 JUnit5 结合数据驱动 -json

作者:测试人
  • 2024-06-26
    北京
  • 本文字数:1876 字

    阅读完需:约 6 分钟

本文转自测试人社区,原文链接:https://ceshiren.com/t/topic/28204

pom 文件导入

<dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-databind</artifactId>    <version>${jackson.version}</version></dependency>
复制代码

解析 list-json

orderlist.json

[  {    "item": "No. 9 Sprockets",    "quantity": 12,    "unitPrice": 1.23,    "orderDate": "2019-04-17"  },  {    "item": "No. Widget (10mm)",    "quantity": 10,    "unitPrice": 3.45,    "orderDate": "2022-01-16"  }]
复制代码

List 类型解析

  • 直接声明类型{style=width:500px}

@Testvoid orderListMap() throws IOException {    ObjectMapper mapper = new ObjectMapper(new JsonFactory());    TypeReference<List<HashMap<String, Object>>> typeReference =            new TypeReference<List<HashMap<String, Object>>>() {};    mapper.findAndRegisterModules();    List<HashMap<String, Object>> listHashMap =            mapper.readValue(                    new File("src/test/resources/json/orderlist.json"),                    typeReference);
System.out.println(listHashMap.toString());}
复制代码

实体类声明

声明实体类 OrderLine

@Datapublic class OrderLine{    private String item;    private int quantity;    private BigDecimal unitPrice;    private LocalDate orderDate;
public OrderLine() { }
public OrderLine(String item, int quantity, BigDecimal unitPrice, LocalDate orderDate) { this.item = item; this.quantity = quantity; this.unitPrice = unitPrice; this.orderDate = orderDate; }}
复制代码

实体类对应解析

@Testvoid orderLinesTest() throws IOException {    ObjectMapper mapper = new ObjectMapper(new JsonFactory());    TypeReference<List<OrderLine>> typeReference =            new TypeReference<List<OrderLine>>() {};    mapper.findAndRegisterModules();    List<OrderLine> orderLines =            mapper.readValue(                    new File("src/test/resources/json/orderlist.json"),                    typeReference);
System.out.println(orderLines);}
复制代码

解析无规律 json

order.json

{  "orderNo": "A001",  "date": "2019-04-17",  "customerName": "Customer, Joe",  "orderLines": [    {      "item": "No. 9 Sprockets",      "quantity": 12,      "unitPrice": 1.23    },    {      "item": "No. Widget (10mm)",      "quantity": 40,      "unitPrice": 3.45    }  ]}
复制代码

类型解析

  • 直接声明类型{style=width:500px}

@Testvoid orderMap() throws IOException {    ObjectMapper mapper = new ObjectMapper(new JsonFactory());    TypeReference<HashMap<String, Object>> typeReference =            new TypeReference<HashMap<String, Object>>() {};    mapper.findAndRegisterModules();    HashMap<String, Object> stringObjectHashMap =            mapper.readValue(                    new File("src/test/resources/json/order.json"),                    typeReference);
System.out.println(stringObjectHashMap.toString());}
复制代码

实体类声明

声明实体类 Order

@Datapublic class Order {    private String orderNo;    private LocalDate date;    private String customerName;    private List<OrderLine> orderLines;

public Order() { }
public Order(String orderNo, LocalDate date, String customerName, List<OrderLine> orderLines) { this.orderNo = orderNo; this.date = date; this.customerName = customerName; this.orderLines = orderLines; }}
复制代码

实体类对应解析

@Testvoid orderTest() throws IOException {    ObjectMapper mapper = new ObjectMapper(new JsonFactory());    TypeReference<Order> typeReference =            new TypeReference<Order>() {};    mapper.findAndRegisterModules();    Order order =            mapper.readValue(                    new File("src/test/resources/json/order.json"),                    typeReference);
System.out.println(order);}
复制代码

软件测试开发免费视频教程分享


发布于: 刚刚阅读数: 5
用户头像

测试人

关注

专注于软件测试开发 2022-08-29 加入

霍格沃兹测试开发学社,测试人社区:https://ceshiren.com/t/topic/22284

评论

发布
暂无评论
软件测试学习笔记丨JUnit5结合数据驱动-json_软件测试_测试人_InfoQ写作社区