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

Throw AssumptionViolatedException when single null is passed to Assum… #1380

Closed
wants to merge 2 commits into from

Conversation

xendo
Copy link

@xendo xendo commented Oct 31, 2016

…e.assumeNotNull(Object... objects) method

Previous implementation threw NullPointerException when single null argument was passed:
java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Arrays$ArrayList.(Arrays.java:3813)
at java.util.Arrays.asList(Arrays.java:3800)

…e.assumeNotNull(Object... objects) method

Previous implementation threw NullPointerException when single null argument was passed:
java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Arrays$ArrayList.<init>(Arrays.java:3813)
at java.util.Arrays.asList(Arrays.java:3800)
kcooney
kcooney previously approved these changes Oct 31, 2016
Copy link
Member

@kcooney kcooney left a comment

Choose a reason for hiding this comment

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

Thanks!

@kcooney
Copy link
Member

kcooney commented Oct 31, 2016

BTW, are you absolutely sure the test would fail without the code changes? I would think you would need to pass in a null array to trigger the new code path

@xendo
Copy link
Author

xendo commented Oct 31, 2016

Kevin,

Out of those two:

    @Test
    public void arraysNull() throws Exception
    {
        Arrays.asList(null);
    }

    @Test
    public void arraysArrayNull() throws Exception
    {
        Object[] objects = {null};
        Arrays.asList(objects);
    }

only the first one fails with:

java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Arrays$ArrayList.(Arrays.java:3813)
at java.util.Arrays.asList(Arrays.java:3800)
at com.amazon.personid.service.test.integration.ArraysTest.arraysNull

@xendo
Copy link
Author

xendo commented Oct 31, 2016

Also it's fair to say that this invocation produces warning:

Warning:(14, 23) java: non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Object for a varargs call
cast to java.lang.Object[] for a non-varargs call and to suppress this warning

More details here: http://stackoverflow.com/a/4028086

@kcooney kcooney dismissed their stale review October 31, 2016 23:17

Based on that stackoverflow thread, I'm tempted to say that the current behavior is fine. It should be rare to explicitly pass in null here

@kcooney
Copy link
Member

kcooney commented Oct 31, 2016

To extrapolate, the following is the only way to get a NPE out of this method:

assumeNotNull(null);

and

Object[] values = null;
assumeNotNull(values);

The first isn't clearly a valid use of this API. The second should be exceedingly rare.

@xendo
Copy link
Author

xendo commented Nov 1, 2016

Kevin,

You are correct. This is much more subtle than I originally thought. If I was using Java I would have never caught this. Reproducing this in Groovy is, however, much simpler:

    def "assumeNotNull should throw AssumptionViolatedException when passed single null value" () {
        given:
        String string = null
        when:
        assumeNotNull(string)
        then:
        thrown AssumptionViolatedException
    }

FYI in Java invocation on any type of Array will throw e.g.

String[] values = null;
assumeNotNull(values);

Copy link
Member

@marcphilipp marcphilipp left a comment

Choose a reason for hiding this comment

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

Thanks! I've added a two inline comments requesting changes.

@@ -111,6 +111,16 @@ public void assumeNotNullThrowsException() {
}

@Test
public void assumeNotNullSingleNullThrowsException() {
try {
assumeNotNull(null);
Copy link
Member

Choose a reason for hiding this comment

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

Please add a cast to Object[] here.

@@ -79,6 +79,7 @@ public static void assumeFalse(String message, boolean b) {
* If called with one or more null elements in <code>objects</code>, the test will halt and be ignored.
Copy link
Member

Choose a reason for hiding this comment

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

Please update the Javadoc:

If called with a {@code null} array or one or more {@code null} elements in {@code objects},
the test will halt and be ignored.

@stefanbirkner stefanbirkner added this to the 4.13 milestone Nov 15, 2016
@stefanbirkner
Copy link
Contributor

Thanks! Could you please update the release notes?

sebasjm pushed a commit to sebasjm/junit4 that referenced this pull request Mar 11, 2018
…e.assumeNotNull(Object... objects) method

Previous implementation threw `NullPointerException` when single `null`
argument was passed:

    java.lang.NullPointerException
      at java.util.Objects.requireNonNull(Objects.java:203)
      at java.util.Arrays$ArrayList.<init>(Arrays.java:3813)
      at java.util.Arrays.asList(Arrays.java:3800)

Fixes junit-team#1380
aristotle0x01 pushed a commit to aristotle0x01/junit4 that referenced this pull request Jun 27, 2022
…e.assumeNotNull(Object... objects) method

Previous implementation threw `NullPointerException` when single `null`
argument was passed:

    java.lang.NullPointerException
      at java.util.Objects.requireNonNull(Objects.java:203)
      at java.util.Arrays$ArrayList.<init>(Arrays.java:3813)
      at java.util.Arrays.asList(Arrays.java:3800)

Fixes junit-team#1380
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.

4 participants