Skip to content

Commit

Permalink
Throw AssumptionViolatedException when single null is passed to Assum…
Browse files Browse the repository at this point in the history
…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
Jerzy Zagorski authored and stefanbirkner committed Nov 15, 2016
1 parent 0138b15 commit 58f1598
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/org/junit/Assume.java
Original file line number Diff line number Diff line change
@@ -76,9 +76,11 @@ 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.
* If called with a {@code null} array or one or more {@code null} elements in {@code objects},
* the test will halt and be ignored.
*/
public static void assumeNotNull(Object... objects) {
assumeThat(objects, notNullValue());
assumeThat(asList(objects), everyItem(notNullValue()));
}

10 changes: 10 additions & 0 deletions src/test/java/org/junit/tests/experimental/AssumptionTest.java
Original file line number Diff line number Diff line change
@@ -110,6 +110,16 @@ public void assumeNotNullThrowsException() {
}
}

@Test
public void assumeNotNullSingleNullThrowsException() {
try {
assumeNotNull((Object[]) null);
fail("should throw AssumptionViolatedException");
} catch (AssumptionViolatedException e) {
// expected
}
}

@Test
public void assumeNotNullPasses() {
Object[] objects = {1, 2};

0 comments on commit 58f1598

Please sign in to comment.