Complex Objects
For more complicated objects, AutoParams utilizes the public constructor with arbitrary arguments to generate test data. If the class has multiple public constructors, the framework will select the one with the fewest parameters to initialize the object.
- Java
- Kotlin
@AllArgsConstructor
@Getter
public class ComplexObject {
private final int value1;
private final String value2;
}
class ComplexObject (val value1: Int, val value2: String)
- Java
- Kotlin
@ParameterizedTest
@AutoSource
void testMethod(ComplexObject arg) {
}
@ParameterizedTest
@AutoSource
fun testMethod(arg: ComplexObject) {
}
Constructor Selection Policy
When AutoParams generates objects of complex types, it adheres to the following selection criteria for constructors:
- Priority is given to constructors that are annotated with the
@ConstructorProperties
annotation. - If no such annotation is present, AutoParams will choose the constructor with the fewest parameters.
- Java
- Kotlin
@Getter
public class ComplexObject {
private final int value1;
private final String value2;
private final UUID value3;
@ConstructorProperties({"value1", "value2, value3"})
public ComplexObject(int value1, String value2, UUID value3) {
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
}
@ConstructorProperties({"value1", "value2"})
public ComplexObject(int value1, String value2) {
this(value1, value2, null);
}
public ComplexObject(int value1) {
this(value1, null, null);
}
}
class ComplexObject
@ConstructorProperties("value1", "value2, value3")
constructor (val value1: Int,
val value2: String?,
val value3: UUID?) {
@ConstructorProperties("value1", "value2")
constructor(value1: Int, value2: String)
: this(value1, value2, null)
constructor(value1: Int): this(value1, null, null)
}
- Java
- Kotlin
@ParameterizedTest
@AutoSource
void testMethod(ComplexObject arg) {
assertNotNull(object.getValue2());
assertNull(object.getValue3());
}
@ParameterizedTest
@AutoSource
fun testMethod(arg: ComplexObject) {
assertNotNull(arg.value2)
assertNull(arg.value3)
}