From 7f9e09dbce778f2b4fc98e762ea61ca695fd425c Mon Sep 17 00:00:00 2001 From: Tobias Knell Date: Tue, 20 Feb 2024 11:43:39 +0100 Subject: [PATCH] Fix creating arrays for constructor arguments --- .../beanfiller/strategies/ArrayStrategy.java | 4 +- .../beanfiller/ConstructorCreationTest.java | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/synyx/beanfiller/ConstructorCreationTest.java diff --git a/src/main/java/org/synyx/beanfiller/strategies/ArrayStrategy.java b/src/main/java/org/synyx/beanfiller/strategies/ArrayStrategy.java index 4f31f0e..5449613 100644 --- a/src/main/java/org/synyx/beanfiller/strategies/ArrayStrategy.java +++ b/src/main/java/org/synyx/beanfiller/strategies/ArrayStrategy.java @@ -72,9 +72,9 @@ public Object createObjectInternal(ObjectInformation objectInformation) throws F private List createObjectsForArray(Class arrayType, Field field, int size, ObjectInformation parentObjectInformation) throws FillingException { - Type type = field.getGenericType(); + Type type = field == null ? null : field.getGenericType(); - if (GenericArrayType.class.isAssignableFrom(type.getClass())) { + if (type != null && GenericArrayType.class.isAssignableFrom(type.getClass())) { // if we have an Array of a Type with Generics, this is needed! type = ((GenericArrayType) type).getGenericComponentType(); } diff --git a/src/test/java/org/synyx/beanfiller/ConstructorCreationTest.java b/src/test/java/org/synyx/beanfiller/ConstructorCreationTest.java new file mode 100644 index 0000000..ad1f4b2 --- /dev/null +++ b/src/test/java/org/synyx/beanfiller/ConstructorCreationTest.java @@ -0,0 +1,64 @@ +package org.synyx.beanfiller; + +import org.junit.Test; +import org.synyx.beanfiller.exceptions.FillingException; +import org.synyx.beanfiller.testobjects.TestEnum; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.notNullValue; + +public class ConstructorCreationTest { + + + @Test + public void createsObjectViaConstructor() throws FillingException { + + TestObjectWithConstructor object = new BeanFiller().fillBean(TestObjectWithConstructor.class); + + assertThat(object.array, notNullValue()); + assertThat(object.array.length, greaterThan(0)); + assertThat(object.testEnum, notNullValue()); + assertThat(object.innerObject, notNullValue()); + assertThat(object.innerObject.foo, notNullValue()); + } + + public static class TestObjectWithConstructor{ + + private final String[] array; + private final TestEnum testEnum; + private final InnerObject innerObject; + + TestObjectWithConstructor(String[] array, TestEnum testEnum, InnerObject innerObject){ + + this.array = array; + this.testEnum = testEnum; + this.innerObject = innerObject; + } + + public String[] getArray() { + return array; + } + + public TestEnum getTestEnum() { + return testEnum; + } + + public InnerObject getInnerObject() { + return innerObject; + } + } + + public static class InnerObject{ + + private final String foo; + + public InnerObject(String foo) { + this.foo = foo; + } + + public String getFoo() { + return foo; + } + } +}