遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

【参考答案】java基础练习:反射

要求

读取所有数据并保存到对象集合

List<TestCase> testcases = new ArrayList<TestCase>();

 

excel用例数据

表单名:testcase

 

参考答案

pom依赖

选下面这个

 

这个依赖了poi

 

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

 

测试用例实体类

package com.qzcsbj;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
public class TestCase {
    private String caseId;
    private String describe;
    private String url;
    private String method;
    private String parameters;
    private String expect;
    private String actual;

    public String getCaseId() {
        return caseId;
    }

    public void setCaseId(String caseId) {
        this.caseId = caseId;
    }

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getParameters() {
        return parameters;
    }

    public void setParameters(String parameters) {
        this.parameters = parameters;
    }

    public String getExpect() {
        return expect;
    }

    public void setExpect(String expect) {
        this.expect = expect;
    }

    public String getActual() {
        return actual;
    }

    public void setActual(String actual) {
        this.actual = actual;
    }

    @Override
    public String toString() {
        return "TestCase{" +
                "caseId='" + caseId + '\'' +
                ", describe='" + describe + '\'' +
                ", url='" + url + '\'' +
                ", method='" + method + '\'' +
                ", parameters='" + parameters + '\'' +
                ", expect='" + expect + '\'' +
                ", actual='" + actual + '\'' +
                '}';
    }
} 

 

实现

package com.qzcsbj;

import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
public class Practice2 {
    public static List<TestCase> testcases = new ArrayList<TestCase>();
    public static void readExcel(String excelPath, String sheetName){
        // 获取类字节码
        Class<TestCase> caseClass = TestCase.class;
        // 创建WorkBook对象
        try {
            Workbook workbook = WorkbookFactory.create(new File(excelPath));
            Sheet sheet = workbook.getSheet(sheetName);
            // 获取第一行,Row是行对象类型
            Row firstRow = sheet.getRow(0);  // 行索引从0开始
            int lastCellNum = firstRow.getLastCellNum();
            // System.out.println(lastCellNum);
            String[] titles = new String[lastCellNum];
            for (int i = 0; i < lastCellNum; i++) {
                Cell cell = firstRow.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                // 设置列的类型为字符串
                // cell.setCellType(CellType.STRING); 
                String title = cell.getStringCellValue();
                // title = title.substring(0,title.indexOf(":"));
                // 值保存到数组
                titles[i] = title;
            }
            // 打印解析出来的标题
            System.out.println(Arrays.toString(titles));

            // 获取sheet最后一行的行号
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 1; i <= lastRowNum  ; i++) {
                // 每行数据一个对象
                TestCase caseObject = caseClass.newInstance();
                // 获取一行数据
                Row rowData = sheet.getRow(i);
                System.out.print("第"+i+"行数据:");

                for (int j = 0; j < lastCellNum ; j++) {
                    Cell cell = rowData.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    cell.setCellType(CellType.STRING);
                    String cellValue = cell.getStringCellValue();
                    System.out.print("【"+ titles[j] + "="+ cellValue+"】");
                    String methodName = "set" + titles[j];
                    Method method = caseClass.getMethod(methodName, String.class);
                    // 反射调用
                    method.invoke(caseObject, cellValue);
                }
                System.out.println();

                // 封装好测试数据的对象添加到集合
                testcases.add(caseObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        readExcel("E:\\case.xlsx","testcase");
        System.out.println("\n===============打印对象=============\n");
        for (TestCase testcase: testcases){
            System.out.println(testcase);
        }
    }
}

 

说明:这种方式只能处理固定对象类型,可以继续优化,使用泛型

 

【java百题计划汇总】

详见:https://www.cnblogs.com/uncleyong/p/15828510.html

 

【bak】

 

原文会持续更新,原文地址:https://www.cnblogs.com/uncleyong/p/17044014.html

 

posted @ 2023-01-14 15:43  全栈测试笔记  阅读(276)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end