Skip to content

Commit 7ab25a4

Browse files
committed
test: nest guard clause assertions
1 parent 14abe3f commit 7ab25a4

File tree

4 files changed

+99
-87
lines changed

4 files changed

+99
-87
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public T create(CustomizationContext customizationContext, Annotation[] annotati
7373

7474
private T populate(CustomizationContext customizationContext) {
7575
var result = context.cached(type, instanceFactory.instantiate(type));
76-
var reflector = new Reflector<>(result)
77-
.validateCustomization(customizationContext, type);
76+
var reflector = new Reflector<>(result).validateCustomization(customizationContext, type);
7877
try {
7978
reflector.getDeclaredFields()
8079
.filter(field -> !customizationContext.getIgnoredFields().contains(field.getName()))

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

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.github.nylle.javafixture.testobjects.interfaces.InterfaceWithoutImplementation;
1010
import org.junit.jupiter.api.BeforeEach;
1111
import org.junit.jupiter.api.DisplayName;
12+
import org.junit.jupiter.api.Nested;
1213
import org.junit.jupiter.api.Test;
1314

1415
import java.io.IOException;
@@ -35,32 +36,36 @@ void setup() {
3536
specimenFactory = new SpecimenFactory(context);
3637
}
3738

38-
@Test
39-
void onlyAbstractTypes() {
40-
assertThatThrownBy(() -> new AbstractSpecimen<>(SpecimenType.fromClass(Map.class), context, specimenFactory))
41-
.isInstanceOf(IllegalArgumentException.class)
42-
.hasMessageContaining("type: " + Map.class.getName());
43-
}
44-
45-
@Test
46-
void typeIsRequired() {
47-
assertThatThrownBy(() -> new AbstractSpecimen<>((SpecimenType) null, context, specimenFactory))
48-
.isInstanceOf(IllegalArgumentException.class)
49-
.hasMessageContaining("type: null");
50-
}
51-
52-
@Test
53-
void contextIsRequired() {
54-
assertThatThrownBy(() -> new AbstractSpecimen<>(SpecimenType.fromClass(InterfaceWithoutImplementation.class), null, specimenFactory))
55-
.isInstanceOf(IllegalArgumentException.class)
56-
.hasMessageContaining("context: null");
57-
}
58-
59-
@Test
60-
void specimenFactoryIsRequired() {
61-
assertThatThrownBy(() -> new AbstractSpecimen<>(SpecimenType.fromClass(InterfaceWithoutImplementation.class), context, null))
62-
.isInstanceOf(IllegalArgumentException.class)
63-
.hasMessageContaining("specimenFactory: null");
39+
@Nested
40+
class WhenConstructing {
41+
42+
@Test
43+
void onlyAbstractTypesAreAllowed() {
44+
assertThatThrownBy(() -> new AbstractSpecimen<>(SpecimenType.fromClass(Map.class), context, specimenFactory))
45+
.isInstanceOf(IllegalArgumentException.class)
46+
.hasMessageContaining("type: " + Map.class.getName());
47+
}
48+
49+
@Test
50+
void typeIsRequired() {
51+
assertThatThrownBy(() -> new AbstractSpecimen<>((SpecimenType) null, context, specimenFactory))
52+
.isInstanceOf(IllegalArgumentException.class)
53+
.hasMessageContaining("type: null");
54+
}
55+
56+
@Test
57+
void contextIsRequired() {
58+
assertThatThrownBy(() -> new AbstractSpecimen<>(SpecimenType.fromClass(InterfaceWithoutImplementation.class), null, specimenFactory))
59+
.isInstanceOf(IllegalArgumentException.class)
60+
.hasMessageContaining("context: null");
61+
}
62+
63+
@Test
64+
void specimenFactoryIsRequired() {
65+
assertThatThrownBy(() -> new AbstractSpecimen<>(SpecimenType.fromClass(InterfaceWithoutImplementation.class), context, null))
66+
.isInstanceOf(IllegalArgumentException.class)
67+
.hasMessageContaining("specimenFactory: null");
68+
}
6469
}
6570

6671
@Test
@@ -107,7 +112,7 @@ void voidAbstractMethodsWillJustReturnVoid() {
107112
}
108113

109114
@Test
110-
void createAbstractClassWithoutConstructor() {
115+
void creatingAbstractClassWithoutConstructorFallsBackToManufacturing() {
111116
SpecimenType<Charset> specimenType = SpecimenType.fromClass(Charset.class);
112117
var sut = new AbstractSpecimen<>(specimenType, context, specimenFactory);
113118
assertThat(context.isCached(specimenType)).isFalse();

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

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,50 @@ void setup() {
3939
specimenFactory = new SpecimenFactory(context);
4040
}
4141

42-
@Test
43-
void typeIsRequired() {
44-
assertThatThrownBy(() -> new GenericSpecimen<>(null, context, specimenFactory))
45-
.isInstanceOf(IllegalArgumentException.class)
46-
.hasMessageContaining("type: null");
47-
}
42+
@Nested
43+
class WhenConstructing {
4844

49-
@Test
50-
void typeMustBeParametrized() {
51-
assertThatThrownBy(() -> new GenericSpecimen<>(SpecimenType.fromClass(Object.class), context, specimenFactory))
52-
.isInstanceOf(IllegalArgumentException.class)
53-
.hasMessageContaining("type: java.lang.Object");
54-
}
45+
@Test
46+
void typeIsRequired() {
47+
assertThatThrownBy(() -> new GenericSpecimen<>(null, context, specimenFactory))
48+
.isInstanceOf(IllegalArgumentException.class)
49+
.hasMessageContaining("type: null");
50+
}
5551

56-
@Test
57-
void typeMustNotBeCollection() {
58-
assertThatThrownBy(() -> new GenericSpecimen<>(SpecimenType.fromClass(Collection.class), context, specimenFactory))
59-
.isInstanceOf(IllegalArgumentException.class)
60-
.hasMessageContaining("type: java.util.Collection");
61-
}
52+
@Test
53+
void typeMustBeParametrized() {
54+
assertThatThrownBy(() -> new GenericSpecimen<>(SpecimenType.fromClass(Object.class), context, specimenFactory))
55+
.isInstanceOf(IllegalArgumentException.class)
56+
.hasMessageContaining("type: java.lang.Object");
57+
}
6258

63-
@Test
64-
void typeMustNotBeMap() {
65-
assertThatThrownBy(() -> new GenericSpecimen<>(SpecimenType.fromClass(Map.class), context, specimenFactory))
66-
.isInstanceOf(IllegalArgumentException.class)
67-
.hasMessageContaining("type: java.util.Map");
68-
}
59+
@Test
60+
void typeMustNotBeCollection() {
61+
assertThatThrownBy(() -> new GenericSpecimen<>(SpecimenType.fromClass(Collection.class), context, specimenFactory))
62+
.isInstanceOf(IllegalArgumentException.class)
63+
.hasMessageContaining("type: java.util.Collection");
64+
}
6965

70-
@Test
71-
void contextIsRequired() {
72-
assertThatThrownBy(() -> new GenericSpecimen<>(SpecimenType.fromClass(Optional.class), null, specimenFactory))
73-
.isInstanceOf(IllegalArgumentException.class)
74-
.hasMessageContaining("context: null");
75-
}
66+
@Test
67+
void typeMustNotBeMap() {
68+
assertThatThrownBy(() -> new GenericSpecimen<>(SpecimenType.fromClass(Map.class), context, specimenFactory))
69+
.isInstanceOf(IllegalArgumentException.class)
70+
.hasMessageContaining("type: java.util.Map");
71+
}
7672

77-
@Test
78-
void specimenFactoryIsRequired() {
79-
assertThatThrownBy(() -> new GenericSpecimen<>(SpecimenType.fromClass(Optional.class), context, null))
80-
.isInstanceOf(IllegalArgumentException.class)
81-
.hasMessageContaining("specimenFactory: null");
73+
@Test
74+
void contextIsRequired() {
75+
assertThatThrownBy(() -> new GenericSpecimen<>(SpecimenType.fromClass(Optional.class), null, specimenFactory))
76+
.isInstanceOf(IllegalArgumentException.class)
77+
.hasMessageContaining("context: null");
78+
}
79+
80+
@Test
81+
void specimenFactoryIsRequired() {
82+
assertThatThrownBy(() -> new GenericSpecimen<>(SpecimenType.fromClass(Optional.class), context, null))
83+
.isInstanceOf(IllegalArgumentException.class)
84+
.hasMessageContaining("specimenFactory: null");
85+
}
8286
}
8387

8488
@Test

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,36 @@ void setup() {
3737
specimenFactory = new SpecimenFactory(context);
3838
}
3939

40-
@Test
41-
void onlyObjectTypes() {
42-
assertThatThrownBy(() -> new ObjectSpecimen<>(SpecimenType.fromClass(Map.class), context, specimenFactory))
43-
.isInstanceOf(IllegalArgumentException.class)
44-
.hasMessageContaining("type: " + Map.class.getName());
45-
}
40+
@Nested
41+
class WhenConstructing {
4642

47-
@Test
48-
void typeIsRequired() {
49-
assertThatThrownBy(() -> new ObjectSpecimen<>(null, context, specimenFactory))
50-
.isInstanceOf(IllegalArgumentException.class)
51-
.hasMessageContaining("type: null");
52-
}
43+
@Test
44+
void onlyObjectTypesAreAllowed() {
45+
assertThatThrownBy(() -> new ObjectSpecimen<>(SpecimenType.fromClass(Map.class), context, specimenFactory))
46+
.isInstanceOf(IllegalArgumentException.class)
47+
.hasMessageContaining("type: " + Map.class.getName());
48+
}
5349

54-
@Test
55-
void contextIsRequired() {
56-
assertThatThrownBy(() -> new ObjectSpecimen<>(SpecimenType.fromClass(Object.class), null, specimenFactory))
57-
.isInstanceOf(IllegalArgumentException.class)
58-
.hasMessageContaining("context: null");
59-
}
50+
@Test
51+
void typeIsRequired() {
52+
assertThatThrownBy(() -> new ObjectSpecimen<>(null, context, specimenFactory))
53+
.isInstanceOf(IllegalArgumentException.class)
54+
.hasMessageContaining("type: null");
55+
}
6056

61-
@Test
62-
void specimenFactoryIsRequired() {
63-
assertThatThrownBy(() -> new ObjectSpecimen<>(SpecimenType.fromClass(Object.class), context, null))
64-
.isInstanceOf(IllegalArgumentException.class)
65-
.hasMessageContaining("specimenFactory: null");
57+
@Test
58+
void contextIsRequired() {
59+
assertThatThrownBy(() -> new ObjectSpecimen<>(SpecimenType.fromClass(Object.class), null, specimenFactory))
60+
.isInstanceOf(IllegalArgumentException.class)
61+
.hasMessageContaining("context: null");
62+
}
63+
64+
@Test
65+
void specimenFactoryIsRequired() {
66+
assertThatThrownBy(() -> new ObjectSpecimen<>(SpecimenType.fromClass(Object.class), context, null))
67+
.isInstanceOf(IllegalArgumentException.class)
68+
.hasMessageContaining("specimenFactory: null");
69+
}
6670
}
6771

6872
@Test

0 commit comments

Comments
 (0)