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

Provide helpful message if parameter cannot be set. #1465

Merged
merged 1 commit into from Jun 23, 2017

Conversation

stefanbirkner
Copy link
Contributor

For private @parameter fields is users get an exception like "java.lang.IllegalAccessException: Class ... can not access a member of class X with modifiers private" The new message "Cannot set parameter 'parameter'. Ensure that the the field 'parameter' is public." tells the user what they should do.

The reason for adding this feature is the Stackoverflow question https://stackoverflow.com/questions/44522046/reflection-exception-in-parameterized-junit-test-using-array-parameter/44522988

@@ -73,6 +73,10 @@ private Object createTestUsingFieldInjection() throws Exception {
int index = annotation.value();
try {
field.set(testClassInstance, parameters[index]);
} catch (IllegalAccessException e) {
throw new Exception("Cannot set parameter '" + field.getName()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure we shouldn't set the cause of this exception?

Also, throwing ``Exceptionis rare. Perhaps we should throw a newIllegalAccessException`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right with the cause. The problem with creating a new IllegalAccessException is that it doesn't have a constructor with a cause argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#initCause(java.lang.Throwable) to set the cause.on the IllegalAccessException. Can we do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And again you're right. I fixed it and already squashed the commits.

@stefanbirkner
Copy link
Contributor Author

When I have your okay for the pull request I will squash the commits and merge to master.

@stefanbirkner stefanbirkner force-pushed the private-parameters branch 2 times, most recently from ad3d20c to 709a705 Compare June 23, 2017 21:10
For private @parameter fields is users get an exception like
"java.lang.IllegalAccessException: Class ... can not access a member
of class X with modifiers private" The new message "Cannot set
parameter 'parameter'. Ensure that the the field 'parameter' is
public." tells the user what they should do.

The reason for adding this feature is the Stackoverflow question
https://stackoverflow.com/questions/44522046/reflection-exception-in-parameterized-junit-test-using-array-parameter/44522988
@stefanbirkner stefanbirkner merged commit 6324fde into junit-team:master Jun 23, 2017
@stefanbirkner stefanbirkner deleted the private-parameters branch June 23, 2017 21:22
@stefanbirkner stefanbirkner added this to the 4.13 milestone Jun 29, 2017
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

Successfully merging this pull request may close these issues.

None yet

2 participants