six2six / fixture-factory

Generator fake objects from a template
Apache License 2.0
445 stars 88 forks source link

Can't instantiate from fixture with null value in a Double field #94

Open felipebernardes opened 7 years ago

felipebernardes commented 7 years ago

Hello folks,

Even so, I'm getting this error when trying to instantiate from a fixture with a null value in a Double field: screen shot 2017-04-18 at 17 18 45

Could you please help me?

Thanks in advance && thanks for the great lib!

jmcomets commented 4 years ago

I'm having a similar issue, when I tried to provide a null string to the fixture, it gives me an empty string.

After debugging this morning seems like a bug in the reflection logic around here.

for (String parameterName : parameterNames) {
    Class<?> fieldType = ReflectionUtils.invokeRecursiveType(templateHolder.getClazz(), parameterName);
    Object result = processedArguments.get(parameterName); // [1]
    if (result == null) { // nulls branch here
        result = processChainedProperty(parameterName, fieldType, processedArguments);
    }
    values.add(transformerChain.transform(result, fieldType));
}

What's happening under the hood is that it considers that it doesn't find the parameter when it is but the value is actually null. Because of this it tries to instantiate the type using its default constructor:

A fix in the above snippet would be to handle when the key isn't present differently than when the value is null (see [1]).