Skip to content

Commit 735e15b

Browse files
akutscheraNylle
authored andcommitted
fix: add remove to context cache #102
We don't need a cached instance after we created an object.
1 parent fbceffb commit 735e15b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,9 @@ public <T> T preDefined(SpecimenType<T> type, T instance) {
5353
return cache.containsKey(type) ? (T) cache.get(type) : instance;
5454
}
5555

56+
public <T> T remove(SpecimenType<T> type) {
57+
return (T) cache.remove(type);
58+
}
59+
5660
}
5761

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.nylle.javafixture;
22

3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Nested;
35
import org.junit.jupiter.api.Test;
46

57
import static org.assertj.core.api.Assertions.assertThat;
@@ -56,6 +58,40 @@ void overwriteWillRemovePreviousValue() {
5658

5759
}
5860

61+
@DisplayName("remove")
62+
@Nested
63+
class RemoveTest {
64+
65+
@DisplayName("will return null if type was not in cache")
66+
@Test
67+
void removeWillReturnNull() {
68+
var sut = new Context(new Configuration());
69+
70+
var actual = sut.remove(new SpecimenType<>() {});
71+
72+
assertThat(actual).isNull();
73+
74+
}
75+
76+
@DisplayName("will return instance from cache if type was in cache")
77+
@Test
78+
void returnCacheInstance() {
79+
var sut = new Context(new Configuration());
80+
var specimenType = new SpecimenType<>() {};
81+
var object = new Object();
82+
83+
sut.cached(specimenType, object);
84+
85+
assertThat(sut.isCached(specimenType)).isTrue();
86+
87+
var actual = sut.remove(specimenType);
88+
89+
assertThat(actual).isSameAs(object);
90+
assertThat(sut.isCached(specimenType)).isFalse();
91+
92+
}
93+
}
94+
5995
class TestObject {
6096
private final String value;
6197

0 commit comments

Comments
 (0)