Skip to content

Commit 1f84194

Browse files
Nylleakutschera
authored andcommitted
test: increase test-coverage
Refs: #106
1 parent eb0096e commit 1f84194

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/test/java/com/github/nylle/javafixture/InstanceFactoryTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.github.nylle.javafixture.testobjects.factorymethod.TestObjectWithNonPublicFactoryMethods;
1313
import com.github.nylle.javafixture.testobjects.interfaces.InterfaceWithDefaultMethod;
1414
import com.github.nylle.javafixture.testobjects.withconstructor.ConstructorExceptionAndNoFactoryMethod;
15+
import com.github.nylle.javafixture.testobjects.withconstructor.ConstructorExceptionAndThrowingFactoryMethod;
1516
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithConstructedField;
1617
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithGenericConstructor;
1718
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithPrivateConstructor;
@@ -125,6 +126,22 @@ void fallbackToFactoryMethodWhenConstructorThrowsException() {
125126
assertThat(result.getValue()).isNotNull();
126127
}
127128

129+
@Test
130+
@DisplayName("will fallback to factory method and fail on exception")
131+
void throwWhenFallbackFails() {
132+
var sut = new InstanceFactory(new SpecimenFactory(new Context(Configuration.configure())));
133+
134+
assertThatExceptionOfType(SpecimenException.class)
135+
.isThrownBy(() -> sut.construct(new SpecimenType<ConstructorExceptionAndThrowingFactoryMethod>() {}, new CustomizationContext(List.of(), Map.of(), false)))
136+
.withMessageContaining("Cannot create instance of class")
137+
.havingCause()
138+
.isInstanceOf(InvocationTargetException.class)
139+
.havingCause()
140+
.isInstanceOf(IllegalArgumentException.class)
141+
.withMessage("expected for tests")
142+
.withNoCause();
143+
}
144+
128145
@Test
129146
@DisplayName("will fallback to factory method and pass exceptions on")
130147
void passExceptionToFallbackWhenConstructorThrows() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.nylle.javafixture.testobjects.withconstructor;
2+
3+
public class ConstructorExceptionAndThrowingFactoryMethod {
4+
public ConstructorExceptionAndThrowingFactoryMethod() {
5+
throw new IllegalArgumentException("expected for tests");
6+
}
7+
public static ConstructorExceptionAndThrowingFactoryMethod factoryMethod() {
8+
throw new IllegalStateException("expected for tests");
9+
}
10+
}

0 commit comments

Comments
 (0)