@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.
- Java
- Kotlin
@AllArgsConstructor
@Getter
public class StringContainer {
private final String value;
}class StringContainer(val value: String)
- Java
- Kotlin
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());
}
}import autoparams.customization.Matching.EXACT_TYPE
class TestClass {
@Test
@AutoParams
fun testMethod(
@FreezeBy(EXACT_TYPE) s1: String,
s2: String,
container: StringContainer
) {
assertSame(s1, s2)
assertSame(s1, container.value)
}
}In this example, all
String
targets—including theString
field insideStringContainer
—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.
- Java
- Kotlin
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());
}
}import autoparams.customization.Matching.IMPLEMENTED_INTERFACES
class TestClass {
@Test
@AutoParams
fun testMethod(
@FreezeBy(IMPLEMENTED_INTERFACES) s1: String,
chars: CharSequence,
container: StringContainer
) {
assertSame(s1, chars)
assertNotSame(s1, container.value)
}
}In this example,
String
implementsCharSequence
, so the same value is reused for boths1
andchars
.StringContainer
is not affected because its type is not an interface. -
PARAMETER_NAME
Reuses the frozen value for other targets with matching names.
- Java
- Kotlin
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());
}
}import autoparams.customization.Matching.PARAMETER_NAME
class TestClass {
@Test
@AutoParams
fun testMethod(
@FreezeBy(PARAMETER_NAME) reviewerId: UUID,
review: Review
) {
assertNotSame(reviewerId, review.id)
assertSame(reviewerId, review.reviewerId)
}
}This strategy is useful when names follow a consistent convention that reflects their role.
noteThis 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.
- Java
- Kotlin
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);
}
}
import autoparams.customization.Matching.EXACT_TYPE
import autoparams.customization.Matching.IMPLEMENTED_INTERFACES
class TestClass {
@Test
@AutoParams
fun testMethod(
@FreezeBy(EXACT_TYPE, IMPLEMENTED_INTERFACES) s1: String,
s2: String,
chars: CharSequence
) {
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.
- Java
- Kotlin
@Test
@AutoParams
void testMethod(@Freeze String s1, String s2) {
assertSame(s1, s2);
}
@Test
@AutoParams
fun testMethod(@Freeze s1: String, s2: String) {
assertSame(s1, s2)
}