idea使用junit测试

这并不是要成为技术含量很高的职位。 这篇文章的目的是为您提供一些指导,以使您的JUnit测试生活更加轻松,使您能够在几分钟内编写复杂的测试场景,并获得具有高度可读性的测试。

单元测试中有两个主要部分,需要编写许多引导程序代码:

  • 设置部分:构建初始状态需要构建将被馈送到SUT(被测系统)的初始对象
  • 断言部分:构造输出对象的所需图像,并仅对所需数据进行断言。

为了降低构建用于测试的对象的复杂性,我建议在以下解释中使用Builder模式:

这是域对象:

public class Employee {private int id;private String name;private Department department;//setters, getters, hashCode, equals, toString methods

此域对象的生成器将如下所示:

public class EmployeeBuilder {private Employee employee;public EmployeeBuilder() {employee = new Employee();}public static EmployeeBuilder defaultValues() {return new EmployeeBuilder();}public static EmployeeBuilder clone(Employee toClone) {EmployeeBuilder builder = defaultValues();builder.setId(toClone.getId());builder.setName(toClone.getName());builder.setDepartment(toClone.getDepartment());return builder;}public static EmployeeBuilder random() {EmployeeBuilder builder = defaultValues();builder.setId(getRandomInteger(0, 1000));builder.setName(getRandomString(20));builder.setDepartment(Department.values()[getRandomInteger(0, Department.values().length - 1)]);return builder;}public EmployeeBuilder setId(int id) {employee.setId(id);return this;}public EmployeeBuilder setName(String name) {employee.setName(name);return this;}public EmployeeBuilder setDepartment(Department dept) {employee.setDepartment(dept);return this;}public Employee build() {return employee;}
}

如您所见,我们有一些工厂方法:

public static EmployeeBuilder defaultValues()public static EmployeeBuilder clone(Employee toClone)public static EmployeeBuilder random()

这些方法返回不同的构建器:

  • defaultValues:每个字段的一些硬编码值(或Java默认值-当前实现)
  • clone:将获取初始对象中的所有值,并使您可以更改其中一些值
  • random:将为每个字段生成随机值。 当您有很多在测试中不需要的字段时非常有用,但是您需要对其进行初始化。 getRandom *方法是在另一个类中静态定义的。

您可以添加其他方法来根据需要初始化构建器。

此外,构建器还可以处理一些不那么容易构建和更改的对象。 例如,让我们稍微更改Employee对象,使其不可变:

public class Employee {private final int id;private final String name;private final Department department;...
}

现在,我们失去了按需更改字段的可能性。 但是使用以下形式的构建器,我们可以在构造对象时重新获得这种可能性:

public class ImmutableEmployeeBuilder {private int id;private String name;private Department department;public ImmutableEmployeeBuilder() {}public static ImmutableEmployeeBuilder defaultValues() {return new ImmutableEmployeeBuilder();}public static ImmutableEmployeeBuilder clone(Employee toClone) {ImmutableEmployeeBuilder builder = defaultValues();builder.setId(toClone.getId());builder.setName(toClone.getName());builder.setDepartment(toClone.getDepartment());return builder;}public static ImmutableEmployeeBuilder random() {ImmutableEmployeeBuilder builder = defaultValues();builder.setId(getRandomInteger(0, 1000));builder.setName(getRandomString(20));builder.setDepartment(Department.values()[getRandomInteger(0, Department.values().length - 1)]);return builder;}public ImmutableEmployeeBuilder setId(int id) {this.id = id;return this;}public ImmutableEmployeeBuilder setName(String name) {this.name = name;return this;}public ImmutableEmployeeBuilder setDepartment(Department dept) {this.department = dept;return this;}public ImmutableEmployee build() {return new ImmutableEmployee(id, name, department);}
}

当我们难以构造对象或需要更改最终字段时,这非常有用。

这是它的最终结果:

没有建设者:

@Testpublic void changeRoleTestWithoutBuilders() {// building the initial stateEmployee employee = new Employee();employee.setId(1);employee.setDepartment(Department.DEVELOPEMENT);employee.setName("John Johnny");// testing the SUTEmployeeManager employeeManager = new EmployeeManager();employeeManager.changeRole(employee, Department.MANAGEMENT);// building the expectationsEmployee expectedEmployee = new Employee();expectedEmployee.setId(employee.getId());expectedEmployee.setDepartment(Department.MANAGEMENT);expectedEmployee.setName(employee.getName());// assertionsassertThat(employee, is(expectedEmployee));}

与建设者:

@Testpublic void changeRoleTestWithBuilders() {// building the initial stateEmployee employee = EmployeeBuilder.defaultValues().setId(1).setName("John Johnny").setDepartment(Department.DEVELOPEMENT).build();// building the expectationsEmployee expectedEmployee = EmployeeBuilder.clone(employee).setDepartment(Department.MANAGEMENT).build();// testing the SUTEmployeeManager employeeManager = new EmployeeManager();employeeManager.changeRole(employee, Department.MANAGEMENT);// assertionsassertThat(employee, is(expectedEmployee));}

如您所见,测试的大小要小得多,对象的构造也变得更加简单(如果代码格式更好,也会更好)。 如果您具有更复杂的域对象(在实际应用程序中,尤其是在遗留代码中),则差异更大。

玩得开心!

参考:来自Java出现日历博客的JCG合作伙伴 Stefan Bulzan 在JUnit测试中使用了Builder模式 。

翻译自: https://www.javacodegeeks.com/2012/12/using-builder-pattern-in-junit-tests.html

idea使用junit测试

idea使用junit测试_在JUnit测试中使用Builder模式相关推荐

  1. 在JUnit测试中使用Builder模式

    这并不是要成为技术含量很高的职位. 这篇文章的目的是为您提供一些指导,以使您的JUnit测试生活更加轻松,使您能够在几分钟内编写复杂的测试场景,并具有易于阅读的测试优势. 单元测试中有两个主要部分,需 ...

  2. junit数据驱动测试_使用Junit和Easytest进行数据驱动的测试

    junit数据驱动测试 在本文中,我们将看到如何使用Junit进行数据驱动的测试. 为此,我将使用一个名为EasyTest的库. 我们知道,对于TestNG,它已内置了数据提供程序. 通过简单的测试, ...

  3. 编写junit 测试_编写JUnit测试的另一种方法(Jasmine方法)

    编写junit 测试 最近,我为一个小型个人项目编写了很多Jasmine测试. 我花了一些时间才终于感到正确地完成了测试. 在此之后,当切换回JUnit测试时,我总是很难过. 由于某种原因,JUnit ...

  4. 编写junit 测试_使用JUnit和Repeat注​​释编写有效的负载测试

    编写junit 测试 EasyTest最近推出了一组新的注释,可帮助其用户编写有效的测试用例. 进入EasyTest的两个主要注释是: 重复 持续时间 今天,我们将讨论重复标注. 一种新的方法级别注释 ...

  5. spock测试_使用Spock测试您的代码

    spock测试 Spock是针对Java和Groovy应用程序的测试和规范框架. Spock是: 极富表现力 简化测试的"给定/何时/然后" 语法 与大多数IDE和CI服务器兼容. ...

  6. 大样品随机双盲测试_训练和测试样品生成

    大样品随机双盲测试 This post aims to explore a step-by-step approach to create a K-Nearest Neighbors Algorith ...

  7. 机器学习中qa测试_机器学习项目测试怎么做?(看实例)

    机器学习交付项目通常包含两部分产物,一部分是机器学习模型,另一部分是机器学习应用系统.机器学习模型是嫁接在应用之上产生价值的.比如:一款预测雷雨天气的APP,它的雷雨预测功能就是由机器学习模型完成的. ...

  8. 单元测试junit参数_使用Junit参数在更少的时间内编写更好的单元测试

    单元测试junit参数 大多数人都知道单元测试的重要性和好处,以及为什么要在进行的项目中使用它们. 而且,大多数人不喜欢在他们从事的项目中编写单元测试. TDD的人当然处于另一面,但根据我的经验,他们 ...

  9. java junit 运行_运行Junit方法项目启动不了

    从控制台看不出任何有用信息,通过JUnit右键"Copy Failure List"将信息拷贝出来 TestStart.start initializationError(org. ...

最新文章

  1. [恢]hdu 2015
  2. 使用google云(GCP)二次利用安装kali Linux(kali browser)
  3. stl的set,multiset, map, multimap, deque, list, stack, queue, priority_queue
  4. Windows安装Python3
  5. c在linux中怎样执行文件,如何在Linux中编译和运行C/C+程序,简单示例教懂你
  6. TechEd2007现场侧记:TechEd的变与不变
  7. Scala 中的文件操作
  8. 【深度学习torch——error】——“xxx.pt is a zip archive(did you mean to use torch.jit.load()?)
  9. POJ1201-Intervals【差分约束,负环,SPFA】
  10. 面试必备:多线程学习(一)
  11. c语言如何编写mysql客户端_【C/C++学院】(23)Mysql数据库编程--C语言编程实现mysql客户端...
  12. 用Proteus学习51单片机之I2C(IIC)总线
  13. 帝国cms纯php调用,帝国CMS模板中:使用php调用最新文章的代码(非灵动和万能标签)...
  14. 2018 Multi-University Training Contest 4: B. Harvest of Apples(分块打表)
  15. WPF事件,路由事件
  16. 【OpenCV学习笔记】【教程翻译】二(车牌识别算法框架)
  17. mysql carnation_RDS mysql5.6 数据库还原到本地
  18. PCB Layout的设计要点
  19. Vi 编辑器常用命令
  20. dell服务器怎么看故障信息,DELL服务器故障码详解

热门文章

  1. java.nio.ByteBuffer用法小结
  2. hashCode到底有什么用?
  3. Spring Boot 发布 jar 包转为 war 包秘籍。
  4. Java Map集合面试题汇总
  5. 【Java】continue和break区别
  6. 好多人都说存储过程很难?认真看这篇文章就够了
  7. 第六章连接和分组查询
  8. 范式 第一 第二 第三范式
  9. idea中Gitlab项目导入导出
  10. 软件测试遇到的异常情况,豪之诺软件测试项目开发中遇到比较多的Bug总结