如何使用@Getter创建实体以测试方法

时间:2019-05-20 16:57:44

标签: java testing

我的应用程序有几个用@Getter注释并正在使用@Builder的实体。我需要创建包含特定数据的客户列表来测试方法。我无法创建OrderDetails列表并将其分配给Order,因为此属性只有一个getter方法。我也尝试模拟对象,但是再次出现无法设置属性的错误。我能够测试该方法的唯一方法是使用带有测试数据的H2数据库。是否有另一种无需使用H2数据库即可测试我的方法的方法?谢谢。

@Getter
@Entity
public class Customer

private Long customerId;
private String firstName;
private String lastName;

 @OneToMany(
  mappedBy = "order",
  cascade = CascadeType.ALL,
  fetch = FetchType.EAGER)
  private List<Order> orders = new ArrayList<>();

@Builder(toBuilder = true)
  public Customer(Long customerID,
      String firstName,
      String lastName,
@Getter
@Entity
public class Order

private Long orderId;
private Long orderLocationId;
private OrderStatus orderStatus;

@ManyToOne
private Customer customer;

@OneToMany(mappedBy = "orderLine", cascade = CascadeType.ALL
private List<OrderDetails> orderDetails;

@Builder(toBuilder = true)
  public Order(OrderStatus orderStatus,
      Long orderLocationId,
      Customer customer,
@Getter
@Entity
public class OrderDetails

private Long orderDetailId;
private Long quantity;
private Instant orderTimestamp = Instant.now();

@ManyToOne
private Order order;

@Builder(toBuilder = true)
  public OrderDetails(
      Integer quantity,
      Order order) {

0 个答案:

没有答案