|
| 1 | +package com.github.nylle.javafixture.instantiation; |
| 2 | + |
| 3 | +import com.github.nylle.javafixture.Configuration; |
| 4 | +import com.github.nylle.javafixture.Context; |
| 5 | +import com.github.nylle.javafixture.CustomizationContext; |
| 6 | +import com.github.nylle.javafixture.SpecimenFactory; |
| 7 | +import com.github.nylle.javafixture.testobjects.TestObject; |
| 8 | +import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithConstructedField; |
| 9 | +import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithGenericConstructor; |
| 10 | +import org.junit.jupiter.api.DisplayName; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +class ConstructorTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + @DisplayName("returns instance") |
| 23 | + void canCreateInstanceFromConstructor() throws NoSuchMethodException { |
| 24 | + var sut = Constructor.create(TestObjectWithGenericConstructor.class.getConstructor(String.class, Optional.class)); |
| 25 | + |
| 26 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of(), Map.of(), false)); |
| 27 | + |
| 28 | + assertThat(actual).isInstanceOf(TestObjectWithGenericConstructor.class); |
| 29 | + assertThat(actual.getValue()).isInstanceOf(String.class); |
| 30 | + assertThat(actual.getInteger()).isInstanceOf(Optional.class); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + @DisplayName("fields not set by constructor are null") |
| 35 | + void fieldsNotSetByConstructorAreNull() throws NoSuchMethodException { |
| 36 | + var sut = Constructor.create(TestObjectWithGenericConstructor.class.getConstructor(String.class, Optional.class)); |
| 37 | + |
| 38 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of(), Map.of(), false)); |
| 39 | + |
| 40 | + assertThat(actual).isInstanceOf(TestObjectWithGenericConstructor.class); |
| 41 | + assertThat(actual.getPrivateField()).isNull(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + @DisplayName("arguments can be customized") |
| 46 | + void argumentsCanBeCustomized() throws NoSuchMethodException { |
| 47 | + var sut = Constructor.create(TestObject.class.getConstructor(String.class, List.class, Map.class)); |
| 48 | + |
| 49 | + // use arg0, because .class files do not store formal parameter names by default |
| 50 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of(), Map.of("arg0", "customized"), true)); |
| 51 | + |
| 52 | + assertThat(actual.getValue()).isEqualTo("customized"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @DisplayName("using constructor is used for all instances") |
| 57 | + void usingConstructorIsRecursive() throws NoSuchMethodException { |
| 58 | + var sut = Constructor.create(TestObjectWithConstructedField.class.getConstructor(int.class, TestObjectWithGenericConstructor.class)); |
| 59 | + |
| 60 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of(), Map.of(), true)); |
| 61 | + |
| 62 | + assertThat(actual).isInstanceOf(TestObjectWithConstructedField.class); |
| 63 | + assertThat(actual.getTestObjectWithGenericConstructor().getPrivateField()).isNull(); |
| 64 | + assertThat(actual.getNotSetByConstructor()).isNull(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @DisplayName("customized arguments are only used for the top level object (no nested objects)") |
| 69 | + void constructorArgumentsAreUsedOnce() throws NoSuchMethodException { |
| 70 | + var sut = Constructor.create(TestObjectWithConstructedField.class.getConstructor(int.class, TestObjectWithGenericConstructor.class)); |
| 71 | + |
| 72 | + // use arg0, because .class files do not store formal parameter names by default |
| 73 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of(), Map.of("arg0", 2), true)); |
| 74 | + |
| 75 | + assertThat(actual.getSetByConstructor()).isEqualTo(2); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + @DisplayName("customized arguments are used for exclusion, too") |
| 80 | + void ignoredConstructorArgsAreRespected() throws NoSuchMethodException { |
| 81 | + var sut = Constructor.create(TestObjectWithConstructedField.class.getConstructor(int.class, TestObjectWithGenericConstructor.class)); |
| 82 | + |
| 83 | + // use arg0, because .class files do not store formal parameter names by default |
| 84 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), new CustomizationContext(List.of("arg0"), Map.of(), true)); |
| 85 | + |
| 86 | + assertThat(actual.getSetByConstructor()).isEqualTo(0); |
| 87 | + } |
| 88 | +} |
0 commit comments