日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在JUnit测试中使用Builder模式

發(fā)布時間:2023/12/3 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在JUnit测试中使用Builder模式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這并不是要成為技術(shù)含量很高的職位。 這篇文章的目的是為您提供一些指導(dǎo),以使您的JUnit測試生活更加輕松,使您能夠在幾分鐘內(nèi)編寫復(fù)雜的測試場景,并具有易于閱讀的測試優(yōu)勢。

單元測試中有兩個主要部分,需要編寫許多引導(dǎo)程序代碼:

  • 設(shè)置部分:構(gòu)建初始狀態(tài)需要構(gòu)建將被饋送到SUT(被測系統(tǒng))的初始對象
  • 斷言部分:構(gòu)造輸出對象的所需圖像,并僅對所需數(shù)據(jù)進行斷言。


為了降低構(gòu)建用于測試的對象的復(fù)雜性,我建議在以下解釋中使用Builder模式:

這是域?qū)ο?#xff1a;

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

此域?qū)ο蟮纳善魅缦滤?#xff1a;

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()

這些方法返回不同的構(gòu)建器:

  • defaultValues:每個字段的一些硬編碼值(或Java默認值-當前實現(xiàn))
  • clone:將獲取初始對象中的所有值,并使您可以更改其中一些值
  • random:將為每個字段生成隨機值。 當您有很多字段在測試中不需要時非常有用,但是您需要將它們初始化。 getRandom *方法是在另一個類中靜態(tài)定義的。

您可以添加其他方法來根據(jù)需要初始化構(gòu)建器。

此外,構(gòu)建器還可以處理一些不那么容易構(gòu)建和更改的對象。 例如,讓我們稍微更改Employee對象,使其不可變:

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

現(xiàn)在,我們失去了按需更改字段的可能性。 但是使用以下形式的構(gòu)建器,我們可以在構(gòu)造對象時重新獲得這種可能性:

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);} }

當我們難以構(gòu)造對象或需要更改最終字段時,這非常有用。

這是它的最終結(jié)果:

沒有建設(shè)者:

@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));}

與建設(shè)者:

@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));}

如您所見,測試的大小要小得多,對象的構(gòu)造也變得更加簡單(如果代碼格式更好,也會更好)。 如果您具有更復(fù)雜的域?qū)ο?#xff08;在實際應(yīng)用程序中,尤其是在遺留代碼中),則差異更大。

玩得開心!

參考:來自Java出現(xiàn)日歷博客的JCG合作伙伴 Stefan Bulzan 在JUnit測試中使用了Builder模式 。

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

總結(jié)

以上是生活随笔為你收集整理的在JUnit测试中使用Builder模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。