1111import com .github .nylle .javafixture .testobjects .factorymethod .GenericClassWithFactoryMethodWithoutArgument ;
1212import com .github .nylle .javafixture .testobjects .factorymethod .TestObjectWithNonPublicFactoryMethods ;
1313import com .github .nylle .javafixture .testobjects .interfaces .InterfaceWithDefaultMethod ;
14+ import com .github .nylle .javafixture .testobjects .withconstructor .ConstructorExceptionAndNoFactoryMethod ;
1415import com .github .nylle .javafixture .testobjects .withconstructor .TestObjectWithConstructedField ;
1516import com .github .nylle .javafixture .testobjects .withconstructor .TestObjectWithGenericConstructor ;
1617import com .github .nylle .javafixture .testobjects .withconstructor .TestObjectWithPrivateConstructor ;
1718import org .junit .jupiter .api .DisplayName ;
1819import org .junit .jupiter .api .Nested ;
1920import org .junit .jupiter .api .Test ;
2021
22+ import java .lang .reflect .InvocationTargetException ;
2123import java .nio .charset .Charset ;
2224import java .util .ArrayDeque ;
2325import java .util .ArrayList ;
@@ -97,7 +99,9 @@ void canOnlyUsePublicConstructor() {
9799 assertThatExceptionOfType (SpecimenException .class )
98100 .isThrownBy (() -> sut .construct (fromClass (TestObjectWithPrivateConstructor .class ), new CustomizationContext (List .of (), Map .of (), false )))
99101 .withMessageContaining ("Cannot create instance of class" )
100- .withNoCause ();
102+ .havingCause ()
103+ .isInstanceOf (SpecimenException .class )
104+ .withMessage ("No public constructor found" );
101105 }
102106
103107 @ Test
@@ -120,6 +124,17 @@ void fallbackToFactoryMethodWhenConstructorThrowsException() {
120124 assertThat (result .getValue ()).isNotNull ();
121125 }
122126
127+ @ Test
128+ @ DisplayName ("will fallback to factory method and pass exceptions on" )
129+ void passExceptionToFallbackWhenConstructorThrows () {
130+ var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
131+
132+ assertThatExceptionOfType (SpecimenException .class )
133+ .isThrownBy (() -> sut .construct (new SpecimenType <ConstructorExceptionAndNoFactoryMethod >() {}, new CustomizationContext (List .of (), Map .of (), false )))
134+ .withMessageContaining ("Cannot create instance of class" )
135+ .withCauseInstanceOf (InvocationTargetException .class );
136+ }
137+
123138 @ Test
124139 @ DisplayName ("arguments can be customized" )
125140 void argumentsCanBeCustomized () {
@@ -178,7 +193,7 @@ class FactoryMethods {
178193 void canCreateInstanceFromAbstractClassUsingFactoryMethod () {
179194 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
180195
181- var actual = sut .manufacture (new SpecimenType <Charset >() {}, noContext ());
196+ var actual = sut .manufacture (new SpecimenType <Charset >() {}, noContext (), null );
182197
183198 assertThat (actual ).isInstanceOf (Charset .class );
184199 }
@@ -189,17 +204,30 @@ void canOnlyUsePublicFactoryMethods() {
189204 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
190205
191206 assertThatExceptionOfType (SpecimenException .class )
192- .isThrownBy (() -> sut .manufacture (fromClass (TestObjectWithNonPublicFactoryMethods .class ), noContext ()))
207+ .isThrownBy (() -> sut .manufacture (fromClass (TestObjectWithNonPublicFactoryMethods .class ), noContext (), null ))
193208 .withMessageContaining ("Cannot create instance of class" )
194209 .withNoCause ();
195210 }
196211
212+ @ Test
213+ @ DisplayName ("includes provided throwable as cause" )
214+ void takesThrowableAsCause () {
215+ var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
216+
217+ var throwable = new RuntimeException ("expected for test" );
218+
219+ assertThatExceptionOfType (SpecimenException .class )
220+ .isThrownBy (() -> sut .manufacture (fromClass (TestObjectWithNonPublicFactoryMethods .class ), noContext (), throwable ))
221+ .withMessageContaining ("Cannot create instance of class" )
222+ .withCause (throwable );
223+ }
224+
197225 @ Test
198226 @ DisplayName ("method arguments are used" )
199227 void factoryMethodWithArgument () {
200228 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
201229
202- FactoryMethodWithArgument result = sut .manufacture (fromClass (FactoryMethodWithArgument .class ), noContext ());
230+ FactoryMethodWithArgument result = sut .manufacture (fromClass (FactoryMethodWithArgument .class ), noContext (), null );
203231
204232 assertThat (result .getValue ()).isNotNull ();
205233 }
@@ -209,7 +237,7 @@ void factoryMethodWithArgument() {
209237 void shouldFilter () {
210238 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
211239
212- FactoryMethodWithItselfAsArgument result = sut .manufacture (fromClass (FactoryMethodWithItselfAsArgument .class ), noContext ());
240+ FactoryMethodWithItselfAsArgument result = sut .manufacture (fromClass (FactoryMethodWithItselfAsArgument .class ), noContext (), null );
213241
214242 assertThat (result .getValue ()).isNull ();
215243 }
@@ -220,7 +248,7 @@ void shouldFailWithoutValidFactoryMethod() {
220248 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
221249
222250 assertThatExceptionOfType (SpecimenException .class )
223- .isThrownBy (() -> sut .manufacture (fromClass (FactoryMethodWithOnlyItselfAsArgument .class ), noContext ()));
251+ .isThrownBy (() -> sut .manufacture (fromClass (FactoryMethodWithOnlyItselfAsArgument .class ), noContext (), null ));
224252
225253 }
226254
@@ -229,7 +257,7 @@ void shouldFailWithoutValidFactoryMethod() {
229257 void createOptional () {
230258 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
231259
232- var result = sut .manufacture (new SpecimenType <Optional <String >>() {}, noContext ());
260+ var result = sut .manufacture (new SpecimenType <Optional <String >>() {}, noContext (), null );
233261
234262 assertThat (result ).isInstanceOf (Optional .class );
235263 assertThat (result .orElse ("optional may be empty" )).isInstanceOf (String .class );
@@ -240,7 +268,7 @@ void createOptional() {
240268 void factoryMethodWithoutArgument () {
241269 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
242270
243- FactoryMethodWithoutArgument result = sut .manufacture (fromClass (FactoryMethodWithoutArgument .class ), noContext ());
271+ FactoryMethodWithoutArgument result = sut .manufacture (fromClass (FactoryMethodWithoutArgument .class ), noContext (), null );
244272
245273 assertThat (result .getValue ()).isEqualTo (42 );
246274 }
@@ -250,7 +278,7 @@ void factoryMethodWithoutArgument() {
250278 void factoryMethodWithGenericArgument () {
251279 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
252280
253- var result = sut .manufacture (new SpecimenType <FactoryMethodWithGenericArgument <Integer >>() {}, noContext ());
281+ var result = sut .manufacture (new SpecimenType <FactoryMethodWithGenericArgument <Integer >>() {}, noContext (), null );
254282
255283 assertThat (result .getValue ()).isNotNull ();
256284 }
@@ -260,7 +288,7 @@ void factoryMethodWithGenericArgument() {
260288 void genericNoArgumentFactoryMethod () {
261289 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
262290
263- var result = sut .manufacture (new SpecimenType <GenericClassWithFactoryMethodWithoutArgument <Integer >>() {}, noContext ());
291+ var result = sut .manufacture (new SpecimenType <GenericClassWithFactoryMethodWithoutArgument <Integer >>() {}, noContext (), null );
264292
265293 assertThat (result ).isNotNull ();
266294 assertThat (result .getValue ()).isEqualTo (42 );
@@ -461,7 +489,7 @@ class ProxyTest {
461489 void callsDefaultMethods () {
462490 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
463491
464- var actual = (InterfaceWithDefaultMethod ) sut .proxy (new SpecimenType <InterfaceWithDefaultMethod >() {}, new HashMap <String , ISpecimen <?> >());
492+ var actual = (InterfaceWithDefaultMethod ) sut .proxy (new SpecimenType <InterfaceWithDefaultMethod >() {}, new HashMap <>());
465493
466494 assertThat (actual .getTestObject ()).isNotNull ();
467495 assertThat (actual .getDefaultInt ()).isEqualTo (42 );
@@ -472,7 +500,7 @@ void callsDefaultMethods() {
472500 void fromAbstractClass () {
473501 var sut = new InstanceFactory (new SpecimenFactory (new Context (Configuration .configure ())));
474502
475- var actual = (AbstractClassWithConcreteMethod ) sut .proxy (new SpecimenType <AbstractClassWithConcreteMethod >() {}, new HashMap <String , ISpecimen <?> >());
503+ var actual = (AbstractClassWithConcreteMethod ) sut .proxy (new SpecimenType <AbstractClassWithConcreteMethod >() {}, new HashMap <>());
476504
477505 assertThat (actual .getTestObject ()).isNotNull ();
478506 assertThat (actual .getDefaultInt ()).isEqualTo (42 );
0 commit comments