Skip to content

Commit 1aa390c

Browse files
akutscheraNylle
authored andcommitted
Use customized fields only once.
Only the top-level object's fields can be customized. By removing the field from the customization context, we ensure it is not used again. Refs: #92
1 parent 70f3d50 commit 1aa390c

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/main/java/com/github/nylle/javafixture/InstanceFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private <T> T construct(final SpecimenType<T> type, final Constructor<?> constru
122122

123123
private Object createParameter(Parameter parameter, CustomizationContext customizationContext) {
124124
if (customizationContext.getCustomFields().containsKey(parameter.getName())) {
125-
return customizationContext.getCustomFields().get(parameter.getName());
125+
return customizationContext.getCustomFields().remove(parameter.getName());
126126
}
127127
var specimen = specimenFactory.build(SpecimenType.fromClass(parameter.getParameterizedType()));
128128
return specimen.create(customizationContext, new Annotation[0]);

src/test/java/com/github/nylle/javafixture/InstanceFactoryTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ void constructorArgumentsAreCached() {
141141
assertThat(first.getStrings()).usingRecursiveComparison().isEqualTo(second.getStrings());
142142
assertThat(first.getValue()).as("primitives are never cached").isNotEqualTo(second.getValue());
143143
}
144+
145+
@Test
146+
@DisplayName("customized arguments are only used for the top level object (no nested objects)")
147+
void constructorArgumentsAreUsedOnce() {
148+
var sut = new InstanceFactory(new SpecimenFactory(new Context(Configuration.configure())));
149+
150+
var customizationContext = new CustomizationContext(true);
151+
customizationContext.getCustomFields().put("arg0", 2);
152+
TestObjectWithConstructedField result = sut.construct(fromClass(TestObjectWithConstructedField.class), customizationContext);
153+
154+
assertThat(result.getSetByConstructor()).isEqualTo(2);
155+
}
144156
}
145157

146158
@Nested

src/test/java/com/github/nylle/javafixture/testobjects/withconstructor/TestObjectWithConstructedField.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ public class TestObjectWithConstructedField {
55

66
private String notSetByConstructor;
77

8+
private final int setByConstructor;
9+
810
private final TestObjectWithGenericConstructor testObjectWithGenericConstructor;
911

1012

11-
public TestObjectWithConstructedField(TestObjectWithGenericConstructor testObjectWithGenericConstructor) {
13+
public TestObjectWithConstructedField(int setByConstructor, TestObjectWithGenericConstructor testObjectWithGenericConstructor) {
14+
this.setByConstructor = setByConstructor;
1215
this.testObjectWithGenericConstructor = testObjectWithGenericConstructor;
1316
}
1417

@@ -19,4 +22,8 @@ public TestObjectWithGenericConstructor getTestObjectWithGenericConstructor() {
1922
public String getNotSetByConstructor() {
2023
return notSetByConstructor;
2124
}
25+
26+
public int getSetByConstructor() {
27+
return setByConstructor;
28+
}
2229
}

0 commit comments

Comments
 (0)