Skip to content

Commit 178310e

Browse files
akutscheraNylle
authored andcommitted
fix: remove constructed objects from cache #125
When we cannot fill an object via reflection (e.g. records), then we need to remove the object from the cache after we created it. We need to do this in order to actually fill sets with more than one member
1 parent 3e7901f commit 178310e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/main/java/com/github/nylle/javafixture/specimen/ObjectSpecimen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private T populate(CustomizationContext customizationContext) {
6868
.build(SpecimenType.fromClass(field.getGenericType()))
6969
.create(customizationContext.newForField(field.getName()), reflector.getFieldAnnotations(field)))));
7070
} catch (SpecimenException ex) {
71-
return context.overwrite(type, instanceFactory.construct(type, customizationContext));
71+
context.overwrite(type, instanceFactory.construct(type, customizationContext));
7272
}
7373
return context.remove(type);
7474
}

src/test/java/com/github/nylle/javafixture/specimen/CollectionSpecimenTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ void createHashSetFromSetInterface() {
141141
assertThat(actual.size()).isEqualTo(2);
142142
}
143143

144+
@Test
145+
void createSetWhenObjectIsCreatedWithConstructor() {
146+
var sut = new CollectionSpecimen<>(new SpecimenType<Set<Throwable>>() {}, context, specimenFactory);
147+
148+
var actual = sut.create(noContext(), new Annotation[0]);
149+
150+
assertThat(actual).isInstanceOf(HashSet.class);
151+
assertThat(actual.stream().allMatch(x -> x.getClass().equals(Throwable.class))).isTrue();
152+
assertThat(actual.size()).isEqualTo(2);
153+
}
154+
144155
@Test
145156
void createLinkedBlockingDequeFromBlockingDequeInterface() {
146157
var sut = new CollectionSpecimen<>(new SpecimenType<BlockingDeque<String>>() {}, context, specimenFactory);

0 commit comments

Comments
 (0)