Skip to content

Commit

Permalink
test: increase test-coverage
Browse files Browse the repository at this point in the history
Refs: #106
  • Loading branch information
Nylle committed Aug 25, 2024
1 parent 736d4a2 commit f0cc7f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ConstructorExceptionAndThrowingFactoryMethod>() {}, 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit f0cc7f2

Please sign in to comment.