autoparams-lombok
autoparams-lombok
is an extension for AutoParams that enhances compatibility with Project Lombok, a popular Java library for reducing boilerplate code.
Install
Maven
For Maven, you can add the following dependency to your pom.xml
:
<dependency>
<groupId>io.github.autoparams</groupId>
<artifactId>autoparams-lombok</artifactId>
<version>11.0.5</version>
</dependency>
Gradle (Groovy)
For Gradle Groovy DSL, use:
testImplementation 'io.github.autoparams:autoparams-lombok:11.0.5'
Gradle (Kotlin)
For Gradle Kotlin DSL, use:
testImplementation("io.github.autoparams:autoparams-lombok:11.0.5")
BuilderCustomizer
Class
If you're using Lombok's @Builder
annotation, the BuilderCustomizer
allows AutoParams to generate arbitrary objects via the builder, making it easier to write tests without manually constructing instances.
Suppose you have an Order
class like this:
- Java
@Builder
@Getter
public class Order {
private final UUID id;
private final UUID productId;
private final Integer quantity;
private final UUID customerId;
private final BigDecimal orderedPriceAmount;
private final String comment;
}
To automatically generate Order
instances, apply BuilderCustomizer
in your test:
- Java
@Test
@AutoParams
@Customization(BuilderCustomizer.class)
void testMethod(Order order) {
assertThat(order.getId()).isNotNull();
assertThat(order.getProductId()).isNotNull();
assertThat(order.getQuantity()).isNotNull();
assertThat(order.getQuantity()).isPositive();
assertThat(order.getCustomerId()).isNotNull();
assertThat(order.getOrderedPriceAmount()).isNotNull();
assertThat(order.getComment()).isNotNull();
}
This eliminates the need to manually configure builder calls, helping you write cleaner and more maintainable tests.
Custom Method Names
If your class uses custom builder method names via Lombok's builderMethodName
and buildMethodName
attributes, you can still use BuilderCustomizer
by extending it to specify those method names.
For example, consider the following Shipment
class:
- Java
@Builder(builderMethodName = "getBuilder", buildMethodName = "create")
@Getter
public class Shipment {
private final UUID id;
private final UUID orderId;
private final String postalCode;
private final String address;
private final Boolean shipped;
}
To make this compatible with AutoParams, define a custom subclass:
- Java
public class ShipmentBuilderCustomizer extends BuilderCustomizer {
public ShipmentBuilderCustomizer() {
super("getBuilder", "create");
}
}
Then apply it in your test:
- Java
@Test
@AutoParams
@Customization(ShipmentBuilderCustomizer.class)
void testMethod(Shipment shipment) {
assertThat(shipment.getId()).isNotNull();
assertThat(shipment.getOrderId()).isNotNull();
assertThat(shipment.getPostalCode()).isNotNull();
assertThat(shipment.getAddress()).isNotNull();
assertThat(shipment.getShipped()).isNotNull();
}
This allows you to use custom builder patterns while still benefiting from automatic object generation.