在Java中测试构建器模式

时间:2018-04-03 15:19:37

标签: java unit-testing builder

我们在代码库中广泛使用构建器模式,构建的对象都具有toBuilder()方法。我想编写一个单元测试,确保toBuilder()方法中没有遗漏任何字段,即对于任何可构建对象,我想要大致像这样的测试

MyClass obj = getTestObjectWithRandomData();
assertEquals(obj, obj.toBuilder().build());

现在,我可以很容易地编写getTestObjectWithRandomData()的基本版本,该版本使用反射将一堆值分配给任何对象的字段。但是,问题是build()经常包含大量的验证检查,例如,如果某个整数不在理智的范围内,则会抛出异常。编写符合所有类特定验证检查的getTestObjectWithRandomData()的通用版本是不可能的。

那么,我该怎样做我想做的事情?我很想将构造和验证代码分成不同的方法,以便测试不会在验证上跳过,但这意味着人们必须记得在对象上调用validate()或其他任何东西他们创造它们之后。不好。

还有其他想法吗?

1 个答案:

答案 0 :(得分:4)

如何使用Lombok?这会是你的选择吗?它将自动生成构建器代码,您再也不用担心它了。 https://projectlombok.org/features/Builder

只需使用@Builder

注释您的课程

使用Lombok

import lombok.Builder;
import lombok.Singular;
import java.util.Set;

@Builder
public class BuilderExample {
  private String name;
  private int age;
  @Singular private Set<String> occupations;
}

Vanilla Java

import java.util.Set;

public class BuilderExample {
  private String name;
  private int age;
  private Set<String> occupations;

  BuilderExample(String name, int age, Set<String> occupations) {
    this.name = name;
    this.age = age;
    this.occupations = occupations;
  }

  public static BuilderExampleBuilder builder() {
    return new BuilderExampleBuilder();
  }

  public static class BuilderExampleBuilder {
    private String name;
    private int age;
    private java.util.ArrayList<String> occupations;

    BuilderExampleBuilder() {
    }

    public BuilderExampleBuilder name(String name) {
      this.name = name;
      return this;
    }

    public BuilderExampleBuilder age(int age) {
      this.age = age;
      return this;
    }

    public BuilderExampleBuilder occupation(String occupation) {
      if (this.occupations == null) {
        this.occupations = new java.util.ArrayList<String>();
      }

      this.occupations.add(occupation);
      return this;
    }

    public BuilderExampleBuilder occupations(Collection<? extends String> occupations) {
      if (this.occupations == null) {
        this.occupations = new java.util.ArrayList<String>();
      }

      this.occupations.addAll(occupations);
      return this;
    }

    public BuilderExampleBuilder clearOccupations() {
      if (this.occupations != null) {
        this.occupations.clear();
      }

      return this;
    }

    public BuilderExample build() {
      // complicated switch statement to produce a compact properly sized immutable set omitted.
      // go to https://projectlombok.org/features/Singular-snippet.html to see it.
      Set<String> occupations = ...;
      return new BuilderExample(name, age, occupations);
    }

    @java.lang.Override
    public String toString() {
      return "BuilderExample.BuilderExampleBuilder(name = " + this.name + ", age = " + this.age + ", occupations = " + this.occupations + ")";
    }
  }
}