11package com .github .nylle .javafixture ;
22
33import com .github .nylle .javafixture .testobjects .TestObjectWithGenericConstructor ;
4- import com .github .nylle .javafixture .testobjects .factorymethod .ConstructorExceptionAndFactoryMethod ;
5- import com .github .nylle .javafixture .testobjects .factorymethod .TestObjectWithNonPublicFactoryMethods ;
64import com .github .nylle .javafixture .testobjects .TestObjectWithPrivateConstructor ;
5+ import com .github .nylle .javafixture .testobjects .factorymethod .ConstructorExceptionAndFactoryMethod ;
76import com .github .nylle .javafixture .testobjects .factorymethod .FactoryMethodWithArgument ;
87import com .github .nylle .javafixture .testobjects .factorymethod .FactoryMethodWithGenericArgument ;
98import com .github .nylle .javafixture .testobjects .factorymethod .FactoryMethodWithoutArgument ;
9+ import com .github .nylle .javafixture .testobjects .factorymethod .GenericClassWithFactoryMethodWithoutArgument ;
10+ import com .github .nylle .javafixture .testobjects .factorymethod .TestObjectWithNonPublicFactoryMethods ;
1011import org .junit .jupiter .api .DisplayName ;
1112import org .junit .jupiter .api .Nested ;
1213import org .junit .jupiter .api .Test ;
3839
3940class InstanceFactoryTest {
4041
41- @ Test
42- void canCreateInstanceFromConstructor () {
43- var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
44-
45- TestObjectWithGenericConstructor result = sut .construct (fromClass (TestObjectWithGenericConstructor .class ));
42+ @ Nested
43+ @ DisplayName ("when using constructor" )
44+ class UsingConstructor {
4645
47- assertThat (result ).isInstanceOf (TestObjectWithGenericConstructor .class );
48- assertThat (result .getValue ()).isInstanceOf (String .class );
49- assertThat (result .getInteger ()).isInstanceOf (Optional .class );
50- assertThat (result .getInteger ()).isPresent ();
51- assertThat (result .getInteger ().get ()).isInstanceOf (Integer .class );
52- }
46+ @ Test
47+ @ DisplayName ("instance is created from random constructor" )
48+ void canCreateInstanceFromConstructor () {
49+ var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
5350
54- @ Test
55- void fieldsNotSetByConstructorAreNull () {
51+ TestObjectWithGenericConstructor result = sut .construct (fromClass (TestObjectWithGenericConstructor .class ));
5652
57- var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
53+ assertThat (result ).isInstanceOf (TestObjectWithGenericConstructor .class );
54+ assertThat (result .getValue ()).isInstanceOf (String .class );
55+ assertThat (result .getInteger ()).isInstanceOf (Optional .class );
56+ assertThat (result .getInteger ()).isPresent ();
57+ assertThat (result .getInteger ().get ()).isInstanceOf (Integer .class );
58+ }
5859
59- TestObjectWithGenericConstructor result = sut .construct (fromClass (TestObjectWithGenericConstructor .class ));
60+ @ Test
61+ @ DisplayName ("fields not set by constructor are null" )
62+ void fieldsNotSetByConstructorAreNull () {
6063
61- assertThat (result ).isInstanceOf (TestObjectWithGenericConstructor .class );
62- assertThat (result .getPrivateField ()).isNull ();
63- }
64+ var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
6465
65- @ Test
66- void canOnlyUsePublicConstructor () {
67- var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
66+ TestObjectWithGenericConstructor result = sut .construct (fromClass (TestObjectWithGenericConstructor .class ));
6867
69- assertThatExceptionOfType (SpecimenException .class )
70- .isThrownBy (() -> sut .construct (fromClass (TestObjectWithPrivateConstructor .class )))
71- .withMessageContaining ("Cannot manufacture class" )
72- .withNoCause ();
73- }
74-
75- @ Test
76- void canCreateInstanceFromAbstractClassUsingFactoryMethod () {
77- var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
68+ assertThat (result ).isInstanceOf (TestObjectWithGenericConstructor .class );
69+ assertThat (result .getPrivateField ()).isNull ();
70+ }
7871
79- var actual = sut .manufacture (new SpecimenType <Charset >() {});
72+ @ Test
73+ @ DisplayName ("construction will fail if no public constructor is available" )
74+ void canOnlyUsePublicConstructor () {
75+ var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
8076
81- assertThat (actual ).isInstanceOf (Charset .class );
82- }
77+ assertThatExceptionOfType (SpecimenException .class )
78+ .isThrownBy (() -> sut .construct (fromClass (TestObjectWithPrivateConstructor .class )))
79+ .withMessageContaining ("Cannot manufacture class" )
80+ .withNoCause ();
81+ }
8382
84- @ Test
85- void useFactoryMethodWhenNoConstructorExists () {
86- var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
83+ @ Test
84+ @ DisplayName ("will fallback to factory method when no public constructor exists" )
85+ void useFactoryMethodWhenNoConstructorExists () {
86+ var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
8787
88- FactoryMethodWithoutArgument result = sut .construct (fromClass (FactoryMethodWithoutArgument .class ));
88+ FactoryMethodWithoutArgument result = sut .construct (fromClass (FactoryMethodWithoutArgument .class ));
8989
90- assertThat (result .getValue ()).isEqualTo (42 );
91- }
90+ assertThat (result .getValue ()).isEqualTo (42 );
91+ }
9292
93- @ Test
94- void fallbackToFactoryMethodWhenConstructorThrowsException () {
95- var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
93+ @ Test
94+ @ DisplayName ("will fallback to factor method when constructor fails" )
95+ void fallbackToFactoryMethodWhenConstructorThrowsException () {
96+ var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
9697
97- ConstructorExceptionAndFactoryMethod result = sut .construct (new SpecimenType <ConstructorExceptionAndFactoryMethod >() {});
98+ var result = sut .construct (new SpecimenType <ConstructorExceptionAndFactoryMethod >() {});
9899
99- assertThat (result .getValue ()).isNotNull ();
100+ assertThat (result .getValue ()).isNotNull ();
101+ }
100102 }
101103
102104 @ Nested
103105 @ DisplayName ("when manufacturing using factory methods" )
104106 class FactoryMethods {
107+
108+ @ Test
109+ @ DisplayName ("factory method from abstract class is used when present" )
110+ void canCreateInstanceFromAbstractClassUsingFactoryMethod () {
111+ var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
112+
113+ var actual = sut .manufacture (new SpecimenType <Charset >() {});
114+
115+ assertThat (actual ).isInstanceOf (Charset .class );
116+ }
117+
105118 @ Test
106119 @ DisplayName ("only public methods will be used" )
107120 void canOnlyUsePublicFactoryMethods () {
@@ -112,7 +125,6 @@ void canOnlyUsePublicFactoryMethods() {
112125 .withMessageContaining ("Cannot manufacture class" )
113126 .withNoCause ();
114127 }
115-
116128 @ Test
117129 @ DisplayName ("method arguments are used" )
118130 void factoryMethodWithArgument () {
@@ -122,6 +134,16 @@ void factoryMethodWithArgument() {
122134
123135 assertThat (result .getValue ()).isNotNull ();
124136 }
137+ @ Test
138+ @ DisplayName ("optional can be created (and may be empty)" )
139+ void createOptional () {
140+ var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
141+
142+ var result = sut .manufacture (new SpecimenType <Optional <String >>(){});
143+
144+ assertThat (result ).isInstanceOf (Optional .class );
145+ assertThat (result .orElse ("optional may be empty" )).isInstanceOf (String .class );
146+ }
125147
126148 @ Test
127149 @ DisplayName ("method without arguments is used" )
@@ -132,17 +154,27 @@ void factoryMethodWithoutArgument() {
132154
133155 assertThat (result .getValue ()).isEqualTo (42 );
134156 }
135-
136157 @ Test
137158 @ DisplayName ("method with generic arguments is used" )
138159 void factoryMethodWithGenericArgument () {
139160 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
140161
141- FactoryMethodWithGenericArgument result = sut .manufacture (new SpecimenType <FactoryMethodWithGenericArgument <Integer >>() {});
162+ var result = sut .manufacture (new SpecimenType <FactoryMethodWithGenericArgument <Integer >>() {});
142163
143164 assertThat (result .getValue ()).isNotNull ();
144165 }
145166
167+ @ Test
168+ @ DisplayName ("method without arguments is used" )
169+ void genericNoArgumentFactoryMethod () {
170+ var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
171+
172+ var result = sut .manufacture (new SpecimenType <GenericClassWithFactoryMethodWithoutArgument <Integer >>() {});
173+
174+ assertThat (result ).isNotNull ();
175+ assertThat (result .getValue ()).isEqualTo (42 );
176+ }
177+
146178 }
147179
148180 @ Nested
0 commit comments