🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
依据开发规范,前期我们配合前台开发,需要根据开发时的测试用例写入供前台测试的对接代码。 # 根据url新建触发器 http://127.0.0.1:8080/javaee/Klass.json -> com.mengyunzhi.javaee.action.KlassAction execute() ``` package com.mengyunzhi.javaee.action; public class KlassAction extends Action { /** * */ private static final long serialVersionUID = 1L; private String name=""; private int page = 1; private int pageSize = 5; private int totalCount = 0; public int getTotalCount() { return totalCount; } public void setName(String name) { this.name = name; } public void setPage(int page) { this.page = page; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public String execute() { return SUCCESS; } } ``` 测试: ![https://box.kancloud.cn/a500593f61369d9f1b234f2a635eeb27_902x1022.png](https://box.kancloud.cn/a500593f61369d9f1b234f2a635eeb27_902x1022.png) ## 新建Klass实体 package com.mengyunzhi.javaee.entity; public class Klass { private Long id = 1L; private String name = "一一班"; private Teacher teacher = new Teacher(1L, "张三", "zhangsan", "zhangsan@yunzhiclub.com", true, "123456"); public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } } 单元测试: ``` // 所有的单元测试均位于test文件夹中 package com.mengyunzhi.javaee.entityTest; import org.junit.Test; import com.mengyunzhi.javaee.entity.Klass; public class KlassTest { @Test public void Init() { Klass klass = new Klass(); System.out.println(klass.toString()); } } ``` 测试结果: ``` Klass [id=1, name=一一班, teacher=Teacher [teacherId=1, name=张三, username=zhangsan, email=zhangsan@yunzhiclub.com, sex=true, password=123456]] ``` ## 完善触发器 根据测试用例,我们完善触发器 ``` package com.mengyunzhi.javaee.action; import java.util.ArrayList; import java.util.List; import com.mengyunzhi.javaee.entity.Klass; public class KlassAction extends Action { /** * */ private static final long serialVersionUID = 1L; private String name=""; private int page = 1; private int pageSize = 5; private int totalCount = 0; private List<Klass> klasses = new ArrayList<Klass>(); public List<Klass> getKlasses() { return klasses; } public int getTotalCount() { return totalCount; } public void setName(String name) { this.name = name; } public void setPage(int page) { this.page = page; } public int getPage() { return this.page; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public String execute() { int i; // 初始化变量,用于计数 // 测试用例一 if (this.name.equals("") && this.page == 1 && this.pageSize == 3) { this.totalCount = 7; // 设置总条数 for (i = 0; i < 3; i++) { //循环加入3条信息 this.klasses.add(new Klass()); } // 测试用例2 } else if (this.name.equals("一") && this.page == 2 && this.pageSize == 2) { this.totalCount = 4; for (i = 0; i < 2; i++) { Klass klass = new Klass(); klass.setName("一班"); this.klasses.add(klass); } } return SUCCESS; } } ``` 测试: 测试用例一: ![https://box.kancloud.cn/431d822625f5c456d0833e8d9ac54104_1318x496.png](https://box.kancloud.cn/431d822625f5c456d0833e8d9ac54104_1318x496.png) ``` var jsonData = JSON.parse(responseBody); tests["code 200"] = jsonData.code === 200; tests["total count is 7"] = jsonData.totalCount === 7; tests["kalss length is 3"] = jsonData.klasses.length === 3; ``` 测试用例二: ![https://box.kancloud.cn/6ab9ab29dfee21544fc09515797bd693_1380x514.png](https://box.kancloud.cn/6ab9ab29dfee21544fc09515797bd693_1380x514.png) ``` var jsonData = JSON.parse(responseBody); tests["code 200"] = jsonData.code === 200; tests["total count is 4"] = jsonData.totalCount === 4; tests["kalss length is 2"] = jsonData.klasses.length === 2; tests["page is 2"] = jsonData.page === 2; ``` > git checkout -f step12.1.3