Skip to main content
Version: 10.x

@FreezeBy Annotation

The @FreezeBy annotation enables fine-grained control over value freezing in tests. It allows you to freeze a single value and reuse it across multiple generation targets that match specific conditions, such as type or name. This helps improve test readability and ensures consistency among generated values.

Matching Strategies

AutoParams provides several matching strategies that determine which targets should receive the frozen value during object generation. The following examples illustrate some strategies:

  • EXACT_TYPE

    Reuses the frozen value for all targets with the exact same type.

    @AllArgsConstructor
    @Getter
    public class StringContainer {

    private final String value;
    }
    import static autoparams.customization.Matching.EXACT_TYPE;

    public class TestClass {

    @Test
    @AutoParams
    void testMethod(
    @FreezeBy(EXACT_TYPE) String s1,
    String s2,
    StringContainer container
    ) {
    assertSame(s1, s2);
    assertSame(s1, container.getValue());
    }
    }

    In this example, all String targets—including the String field inside StringContainer—are generated with the same frozen value.

  • IMPLEMENTED_INTERFACES

    Reuses the frozen value for targets whose types are interfaces that the frozen value's type implements.

    import static autoparams.customization.Matching.IMPLEMENTED_INTERFACES;

    public class TestClass {

    @Test
    @AutoParams
    void testMethod(
    @FreezeBy(IMPLEMENTED_INTERFACES) String s1,
    CharSequence chars,
    StringContainer container
    ) {
    assertSame(s1, chars);
    assertNotSame(s1, container.getValue());
    }
    }

    In this example, String implements CharSequence, so the same value is reused for both s1 and chars. StringContainer is not affected because its type is not an interface.

  • PARAMETER_NAME

    Reuses the frozen value for other targets with matching names.

    import static autoparams.customization.Matching.PARAMETER_NAME;

    public class TestClass {

    @Test
    @AutoParams
    void testMethod(
    @FreezeBy(PARAMETER_NAME) UUID reviewerId,
    Review review
    ) {
    assertNotSame(reviewerId, review.getId());
    assertSame(reviewerId, review.getReviewerId());
    }
    }

    This strategy is useful when names follow a consistent convention that reflects their role.


    note

    This feature depends on parameter name availability. See the note in the One-time Customizations with DSL section for details.

You can also combine multiple matching strategies to broaden the scope of freezing.

import static autoparams.customization.Matching.EXACT_TYPE;
import static autoparams.customization.Matching.IMPLEMENTED_INTERFACES;

public class TestClass {

@Test
@AutoParams
void testMethod(
@FreezeBy({ EXACT_TYPE, IMPLEMENTED_INTERFACES }) String s1,
String s2,
CharSequence chars
) {
assertSame(s1, s2);
assertSame(s1, chars);
}
}

In this example, the frozen value s1 is reused for both s2 (same type) and chars (interface implemented by String).

Shorthand for EXACT_TYPE

Using @Freeze is equivalent to @FreezeBy(EXACT_TYPE). It's a convenient shorthand for the most common matching strategy.

@Test
@AutoParams
void testMethod(@Freeze String s1, String s2) {
assertSame(s1, s2);
}