Skip to content

Commit b501719

Browse files
akutscheraNylle
authored andcommitted
feat: omit fields by class type #121
Now you can do "without(String.class)" and you will not have any field set that is of this type.
1 parent 8a60c21 commit b501719

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.nylle.javafixture;
22

3+
import java.util.HashMap;
34
import java.util.Map;
45
import java.util.concurrent.ConcurrentHashMap;
56

@@ -24,7 +25,7 @@ public Context(Configuration configuration, Map<SpecimenType<?>, Object> predefi
2425
}
2526

2627
this.configuration = configuration;
27-
this.cache = new ConcurrentHashMap<>(predefinedInstances);
28+
this.cache = new HashMap<>(predefinedInstances);
2829
}
2930

3031
public Configuration getConfiguration() {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ public interface ISpecimenBuilder<T> {
2222
<U> ISpecimenBuilder<T> with(SpecimenType<U> type, U value);
2323

2424
ISpecimenBuilder<T> without(String fieldName);
25+
26+
<U> ISpecimenBuilder<T> without(Class<U> fieldName);
2527
}
2628

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.List;
77
import java.util.Map;
88
import java.util.Optional;
9-
import java.util.concurrent.ConcurrentHashMap;
109
import java.util.function.Consumer;
1110
import java.util.stream.IntStream;
1211
import java.util.stream.Stream;
@@ -15,7 +14,7 @@ public class SpecimenBuilder<T> implements ISpecimenBuilder<T> {
1514
private final List<Consumer<T>> functions = new LinkedList<>();
1615
private final List<String> ignoredFields = new LinkedList<>();
1716
private final Map<String, Object> customFields = new HashMap<>();
18-
private final Map<SpecimenType<?>, Object> predefinedInstances = new ConcurrentHashMap<>();
17+
private final Map<SpecimenType<?>, Object> predefinedInstances = new HashMap<>();
1918

2019
private final SpecimenType<T> type;
2120
private final Configuration configuration;
@@ -129,6 +128,12 @@ public ISpecimenBuilder<T> without(final String fieldName) {
129128
return this;
130129
}
131130

131+
@Override
132+
public <U> ISpecimenBuilder<T> without(Class<U> fieldName) {
133+
predefinedInstances.put(SpecimenType.fromClass(fieldName), null);
134+
return this;
135+
}
136+
132137
T construct() {
133138
return new SpecimenFactory(new Context(configuration)).build(type).create(new CustomizationContext(List.of(), Map.of(), true), new Annotation[0]);
134139
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ void canOmitPrivateField() {
217217
assertThat(result.getHello()).isNull();
218218
}
219219

220+
@Test
221+
void canOmitFieldsOfClassByType() {
222+
var fixture = new Fixture(configuration);
223+
224+
TestPrimitive result = fixture.build(TestPrimitive.class).without(String.class).create();
225+
226+
assertThat(result.getHello()).isNull();
227+
}
228+
220229
@Test
221230
void canCustomizeOptional() {
222231
Fixture fixture = new Fixture(configuration);

0 commit comments

Comments
 (0)