Search code examples
javaspring-bootbuilder

Class doesn't inherit properties of a class it extends from


I have this Renew class:

@Getter
@Setter
@ToString
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Builder(toBuilder = true)
@EqualsAndHashCode
public class A {
  private ItemInfo itemInfo;

  public boolean hasErrors() {
    return (itemInfo!= null && itemInfo.hasErrors());
  }
}
@Getter
@Setter
@ToString
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Builder(toBuilder = true)
@EqualsAndHashCode
public class B extends A {

  private Price bestPrice;
  private Price alternativePrice;
}

My test class has:

mocked =
    B.builder()
        .itemInfo(null)
        .bestPrice(null)
        .alternativePrice(null)
        .build();

I get an error saying Create method 'itemInfo' in 'BBuilder in B'. I thought that given B extends A, B should contain all that A has. How do I resolve this?


Solution

  • You could try out @SuperBuilder (experimental)

    https://projectlombok.org/features/experimental/SuperBuilder