Skip to content

Commit

Permalink
Fix generating record constructor properties (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Nov 16, 2023
1 parent fcfbf20 commit 53dd62b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.navercorp.fixturemonkey.api.property;

import java.beans.ConstructorProperties;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;

Expand All @@ -31,7 +32,9 @@ public final class DefaultPropertyGenerator implements PropertyGenerator {
new CompositePropertyGenerator(
Arrays.asList(
new ConstructorParameterPropertyGenerator(
it -> it.getAnnotation(ConstructorProperties.class) != null,
it -> it.getAnnotation(ConstructorProperties.class) != null
|| Arrays.stream(it.getParameters()).anyMatch(Parameter::isNamePresent)
|| it.getParameters().length == 0,
it -> true
),
new FieldPropertyGenerator(it -> true, it -> true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.navercorp.fixturemonkey.tests.java17.RecordTestSpecs.ContainerRecord;
import com.navercorp.fixturemonkey.tests.java17.RecordTestSpecs.DateTimeRecord;
import com.navercorp.fixturemonkey.tests.java17.RecordTestSpecs.JavaTypeRecord;
import com.navercorp.fixturemonkey.tests.java17.RecordTestSpecs.NoArgsConstructorRecord;
import com.navercorp.fixturemonkey.tests.java17.RecordTestSpecs.TwoConstructorsRecord;

class ConstructorPropertiesRecordTest {
private static final FixtureMonkey SUT = FixtureMonkey.builder()
Expand Down Expand Up @@ -98,4 +100,41 @@ void fixedInterfaceContainer() {

then(actual).isNotNull();
}

@RepeatedTest(TEST_COUNT)
void sampleTwoConstructorsRecord() {
String actual = SUT.giveMeBuilder(TwoConstructorsRecord.class)
.setNotNull("string")
.sample()
.string();

then(actual).isNotNull();
}

@RepeatedTest(TEST_COUNT)
void fixedTwoConstructorsRecord() {
String actual = SUT.giveMeBuilder(TwoConstructorsRecord.class)
.setNotNull("string")
.fixed()
.sample()
.string();

then(actual).isNotNull();
}

@RepeatedTest(TEST_COUNT)
void sampleNoArgsConstructorRecord() {
NoArgsConstructorRecord actual = SUT.giveMeOne(NoArgsConstructorRecord.class);

then(actual).isNotNull();
}

@RepeatedTest(TEST_COUNT)
void fixedNoArgsConstructorRecord() {
NoArgsConstructorRecord actual = SUT.giveMeBuilder(NoArgsConstructorRecord.class)
.fixed()
.sample();

then(actual).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,15 @@ public record ComplexContainerRecord(
Stream<String> stream
) {
}

public record NoArgsConstructorRecord() {
}

public record TwoConstructorsRecord(
String string
) {
public TwoConstructorsRecord(Integer integer) {
this(String.valueOf(integer));
}
}
}

0 comments on commit 53dd62b

Please sign in to comment.