|
| 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.SpecimenFactory; |
| 6 | +import com.github.nylle.javafixture.SpecimenType; |
| 7 | +import com.github.nylle.javafixture.testobjects.factorymethod.FactoryMethodWithArgument; |
| 8 | +import com.github.nylle.javafixture.testobjects.factorymethod.FactoryMethodWithGenericArgument; |
| 9 | +import com.github.nylle.javafixture.testobjects.factorymethod.FactoryMethodWithoutArgument; |
| 10 | +import com.github.nylle.javafixture.testobjects.factorymethod.GenericClassWithFactoryMethodWithoutArgument; |
| 11 | +import org.junit.jupiter.api.DisplayName; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import java.nio.charset.Charset; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +import static com.github.nylle.javafixture.CustomizationContext.noContext; |
| 18 | +import static com.github.nylle.javafixture.SpecimenType.fromClass; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +class FactoryMethodTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + @DisplayName("returns instance of class using factory method without arguments") |
| 25 | + void factoryMethodWithoutArgument() throws NoSuchMethodException { |
| 26 | + var sut = FactoryMethod.<FactoryMethodWithoutArgument>create(FactoryMethodWithoutArgument.class.getDeclaredMethod("factoryMethod"), fromClass(FactoryMethodWithoutArgument.class)); |
| 27 | + |
| 28 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); |
| 29 | + |
| 30 | + assertThat(actual.getValue()).isEqualTo(42); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + @DisplayName("returns instance of class using factory method with arguments") |
| 35 | + void returnsInstanceOfClassUsingFactoryMethodWithArguments() throws NoSuchMethodException { |
| 36 | + var sut = FactoryMethod.<FactoryMethodWithArgument>create(FactoryMethodWithArgument.class.getDeclaredMethod("factoryMethod", int.class), fromClass(FactoryMethodWithArgument.class)); |
| 37 | + |
| 38 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); |
| 39 | + |
| 40 | + assertThat(actual.getValue()).isNotNull(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + @DisplayName("returns instance of class using factory method with generic argument") |
| 45 | + void factoryMethodWithGenericArgument() throws NoSuchMethodException { |
| 46 | + var sut = FactoryMethod.create(FactoryMethodWithGenericArgument.class.getDeclaredMethod("factoryMethod", Object.class), new SpecimenType<FactoryMethodWithGenericArgument<Integer>>() {}); |
| 47 | + |
| 48 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); |
| 49 | + |
| 50 | + assertThat(actual.getValue()).isNotNull(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @DisplayName("returns instance of abstract class using factory method without arguments") |
| 55 | + void returnsInstanceOfAbstractClassUsingFactoryMethod() throws NoSuchMethodException { |
| 56 | + var sut = FactoryMethod.create(Charset.class.getDeclaredMethod("defaultCharset"), new SpecimenType<Charset>() {}); |
| 57 | + |
| 58 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); |
| 59 | + |
| 60 | + assertThat(actual).isInstanceOf(Charset.class); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName("returns instance of Optional using factory method with arguments") |
| 65 | + void createOptionalWithArgument() throws NoSuchMethodException { |
| 66 | + var sut = FactoryMethod.create(Optional.class.getDeclaredMethod("of", Object.class), new SpecimenType<Optional<String>>() {}); |
| 67 | + |
| 68 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); |
| 69 | + |
| 70 | + assertThat(actual).isInstanceOf(Optional.class); |
| 71 | + assertThat(actual).isNotEmpty(); |
| 72 | + assertThat(actual.get()).isInstanceOf(String.class); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @DisplayName("returns instance of Optional using factory method without arguments") |
| 77 | + void createOptionalWithoutArgument() throws NoSuchMethodException { |
| 78 | + var sut = FactoryMethod.create(Optional.class.getDeclaredMethod("empty"), new SpecimenType<Optional<String>>() {}); |
| 79 | + |
| 80 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); |
| 81 | + |
| 82 | + assertThat(actual).isInstanceOf(Optional.class); |
| 83 | + assertThat(actual).isEmpty(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + @DisplayName("returns instance of generic class using generic method without arguments") |
| 88 | + void genericNoArgumentFactoryMethod() throws NoSuchMethodException { |
| 89 | + var sut = FactoryMethod.create(GenericClassWithFactoryMethodWithoutArgument.class.getDeclaredMethod("factoryMethod"), new SpecimenType<GenericClassWithFactoryMethodWithoutArgument<Integer>>() {}); |
| 90 | + |
| 91 | + var actual = sut.invoke(new SpecimenFactory(new Context(Configuration.configure())), noContext()); |
| 92 | + |
| 93 | + assertThat(actual).isNotNull(); |
| 94 | + assertThat(actual.getValue()).isEqualTo(42); |
| 95 | + } |
| 96 | +} |
0 commit comments