diff --git a/src/test/java/com/github/nylle/javafixture/InstanceFactoryTest.java b/src/test/java/com/github/nylle/javafixture/InstanceFactoryTest.java index 7ab4421..f52dc10 100644 --- a/src/test/java/com/github/nylle/javafixture/InstanceFactoryTest.java +++ b/src/test/java/com/github/nylle/javafixture/InstanceFactoryTest.java @@ -12,6 +12,7 @@ import com.github.nylle.javafixture.testobjects.factorymethod.TestObjectWithNonPublicFactoryMethods; import com.github.nylle.javafixture.testobjects.interfaces.InterfaceWithDefaultMethod; import com.github.nylle.javafixture.testobjects.withconstructor.ConstructorExceptionAndNoFactoryMethod; +import com.github.nylle.javafixture.testobjects.withconstructor.ConstructorExceptionAndThrowingFactoryMethod; import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithConstructedField; import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithGenericConstructor; import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithPrivateConstructor; @@ -125,6 +126,22 @@ void fallbackToFactoryMethodWhenConstructorThrowsException() { assertThat(result.getValue()).isNotNull(); } + @Test + @DisplayName("will fallback to factory method and fail on exception") + void throwWhenFallbackFails() { + var sut = new InstanceFactory(new SpecimenFactory(new Context(Configuration.configure()))); + + assertThatExceptionOfType(SpecimenException.class) + .isThrownBy(() -> sut.construct(new SpecimenType() {}, new CustomizationContext(List.of(), Map.of(), false))) + .withMessageContaining("Cannot create instance of class") + .havingCause() + .isInstanceOf(InvocationTargetException.class) + .havingCause() + .isInstanceOf(IllegalArgumentException.class) + .withMessage("expected for tests") + .withNoCause(); + } + @Test @DisplayName("will fallback to factory method and pass exceptions on") void passExceptionToFallbackWhenConstructorThrows() { diff --git a/src/test/java/com/github/nylle/javafixture/testobjects/withconstructor/ConstructorExceptionAndThrowingFactoryMethod.java b/src/test/java/com/github/nylle/javafixture/testobjects/withconstructor/ConstructorExceptionAndThrowingFactoryMethod.java new file mode 100644 index 0000000..7254880 --- /dev/null +++ b/src/test/java/com/github/nylle/javafixture/testobjects/withconstructor/ConstructorExceptionAndThrowingFactoryMethod.java @@ -0,0 +1,10 @@ +package com.github.nylle.javafixture.testobjects.withconstructor; + +public class ConstructorExceptionAndThrowingFactoryMethod { + public ConstructorExceptionAndThrowingFactoryMethod() { + throw new IllegalArgumentException("expected for tests"); + } + public static ConstructorExceptionAndThrowingFactoryMethod factoryMethod() { + throw new IllegalStateException("expected for tests"); + } +}