{And|Not|Or}PredicateTest: Don't use ExpectedException

Calling ExpectedException.expect multiple times within a test does
not work. After the first one, the subsequent tests will be silently
skipped when the expected exception is raised.

Switch back to using the try-catch pattern, but add calls to fail()
that were not there before, to cause the tests to fail when the
expected exception is not raised.

Also fix a couple of copy-paste mistakes in the assert descriptions.

Change-Id: I1dd30f9fefbf0438d1676aa4ce2cc369b22e1627
This commit is contained in:
David Pursehouse
2015-11-10 09:57:07 -08:00
parent bc0efc330e
commit 51381e71f2
3 changed files with 53 additions and 20 deletions

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
@@ -43,17 +44,29 @@ public class AndPredicateTest extends PredicateTest {
final TestPredicate b = f("author", "bob");
final Predicate<String> n = and(a, b);
exception.expect(UnsupportedOperationException.class);
n.getChildren().clear();
try {
n.getChildren().clear();
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// Expected
}
assertChildren("clear", n, of(a, b));
exception.expect(UnsupportedOperationException.class);
n.getChildren().remove(0);
try {
n.getChildren().remove(0);
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// Expected
}
assertChildren("remove(0)", n, of(a, b));
exception.expect(UnsupportedOperationException.class);
n.getChildren().iterator().remove();
assertChildren("remove(0)", n, of(a, b));
try {
n.getChildren().iterator().remove();
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// Expected
}
assertChildren("iterator().remove()", n, of(a, b));
}
private static void assertChildren(String o, Predicate<String> p,

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
@@ -103,12 +104,18 @@ public class NotPredicateTest extends PredicateTest {
assertNotSame(n, n.copy(sb));
assertEquals(sb, n.copy(sb).getChildren());
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Expected exactly one child");
n.copy(Collections.<Predicate> emptyList());
try {
n.copy(Collections.<Predicate> emptyList());
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("Expected exactly one child", e.getMessage());
}
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Expected exactly one child");
n.copy(and(a, b).getChildren());
try {
n.copy(and(a, b).getChildren());
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("Expected exactly one child", e.getMessage());
}
}
}

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
@@ -43,17 +44,29 @@ public class OrPredicateTest extends PredicateTest {
final TestPredicate b = f("author", "bob");
final Predicate<String> n = or(a, b);
exception.expect(UnsupportedOperationException.class);
n.getChildren().clear();
try {
n.getChildren().clear();
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// Expected
}
assertChildren("clear", n, of(a, b));
exception.expect(UnsupportedOperationException.class);
n.getChildren().remove(0);
try {
n.getChildren().remove(0);
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// Expected
}
assertChildren("remove(0)", n, of(a, b));
exception.expect(UnsupportedOperationException.class);
n.getChildren().iterator().remove();
assertChildren("remove(0)", n, of(a, b));
try {
n.getChildren().iterator().remove();
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// Expected
}
assertChildren("iterator().remove()", n, of(a, b));
}
private static void assertChildren(String o, Predicate<String> p,