Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix creating arrays for constructor arguments #41

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Loading