Skip to content

Commit d1dbae8

Browse files
committed
fix: returns exception message from invocation
1 parent f079468 commit d1dbae8

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/main/java/com/github/nylle/javafixture/instantiation/FactoryMethodInstantiator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.github.nylle.javafixture.SpecimenType;
66

77
import java.lang.annotation.Annotation;
8+
import java.lang.reflect.InvocationTargetException;
89
import java.lang.reflect.Method;
910
import java.util.ArrayList;
1011
import java.util.List;
@@ -33,6 +34,8 @@ public Result<T> invoke(SpecimenFactory specimenFactory, CustomizationContext cu
3334
arguments.add(specimen.create(customizationContext, new Annotation[0]));
3435
}
3536
return Result.of((T) factoryMethod.invoke(null, arguments.toArray()));
37+
} catch (InvocationTargetException ex) {
38+
return Result.empty(ex.getTargetException().getMessage());
3639
} catch (Exception ex) {
3740
return Result.empty(ex.getMessage());
3841
}

src/test/java/com/github/nylle/javafixture/instantiation/ConstructorInstantiatorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ void ignoredConstructorArgsAreRespected() throws NoSuchMethodException {
8888
}
8989

9090
@Test
91-
void xxx() throws NoSuchMethodException {
91+
@DisplayName("returns exception message from invocation when constructor fails")
92+
void resolveTargetExceptionMessage() throws NoSuchMethodException {
9293
var sut = ConstructorInstantiator.create(ConstructorExceptionAndNoFactoryMethod.class.getConstructor());
9394

9495
var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of(), Map.of(), true));

src/test/java/com/github/nylle/javafixture/instantiation/FactoryMethodInstantiatorTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.github.nylle.javafixture.testobjects.factorymethod.FactoryMethodWithGenericArgument;
99
import com.github.nylle.javafixture.testobjects.factorymethod.FactoryMethodWithoutArgument;
1010
import com.github.nylle.javafixture.testobjects.factorymethod.GenericClassWithFactoryMethodWithoutArgument;
11+
import com.github.nylle.javafixture.testobjects.withconstructor.ConstructorExceptionAndThrowingFactoryMethod;
1112
import org.junit.jupiter.api.DisplayName;
1213
import org.junit.jupiter.api.Test;
1314

@@ -93,4 +94,14 @@ void genericNoArgumentFactoryMethod() throws NoSuchMethodException {
9394
assertThat(actual.getValue()).isNotNull();
9495
assertThat(actual.getValue().getValue()).isEqualTo(42);
9596
}
97+
98+
@Test
99+
@DisplayName("returns exception message from invocation when factory method fails")
100+
void resolveTargetExceptionMessage() throws NoSuchMethodException {
101+
var sut = FactoryMethodInstantiator.<FactoryMethodWithoutArgument>create(ConstructorExceptionAndThrowingFactoryMethod.class.getDeclaredMethod("factoryMethod"), fromClass(ConstructorExceptionAndThrowingFactoryMethod.class));
102+
103+
var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext());
104+
105+
assertThat(actual.getMessage()).isEqualTo("factory method exception expected for tests");
106+
}
96107
}

0 commit comments

Comments
 (0)