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

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

Open
felipebernardes opened this issue Apr 18, 2017 · 1 comment
Open

Comments

@felipebernardes
Copy link

Hello folks,

  • when I instantiate from the same fixture with a valid Double value, it works
  • the attribute name was typed correctly, I double checked it

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
Copy link

jmcomets commented Oct 2, 2020

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:

  • in the case of Double, there's no default constructor -> instantiation by reflection fails with the aforementioned error
  • in the case of String, there is a default constructor that returns an empty string (what I'm seeing)

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]).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants