Skip to content

Commit

Permalink
Merge pull request #41 from synyx/fix-constructor-creation-with-arrays
Browse files Browse the repository at this point in the history
Fix creating arrays for constructor arguments
  • Loading branch information
tknell authored Feb 20, 2024
2 parents 8f7c3c1 + 7f9e09d commit d6f722c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public Object createObjectInternal(ObjectInformation objectInformation) throws F
private <T> List<T> createObjectsForArray(Class<T> 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();
}
Expand Down
64 changes: 64 additions & 0 deletions src/test/java/org/synyx/beanfiller/ConstructorCreationTest.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit d6f722c

Please sign in to comment.