From 8cb5f67c584cbcadd50fb4eead2f6b8983f19951 Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Wed, 19 Sep 2012 00:31:53 +0000 Subject: [PATCH] Now we are using JUnit4, can use expected=throwable.class for simple failure checks Note: not suitable for cases where an earlier statement can generate the same exception. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1387417 13f79535-47bb-0310-9956-ffa450edef68 --- .../lang3/builder/CompareToBuilderTest.java | 24 +++++-------------- .../lang3/builder/ToStringBuilderTest.java | 7 +----- .../CallableBackgroundInitializerTest.java | 19 ++++----------- 3 files changed, 11 insertions(+), 39 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/builder/CompareToBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/CompareToBuilderTest.java index 190d1b1bc..77d7edba4 100644 --- a/src/test/java/org/apache/commons/lang3/builder/CompareToBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/CompareToBuilderTest.java @@ -18,7 +18,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.math.BigInteger; @@ -105,25 +104,17 @@ public void testReflectionCompare() { assertTrue(CompareToBuilder.reflectionCompare(o2, o1) > 0); } - @Test + @Test(expected=NullPointerException.class) public void testReflectionCompareEx1() { TestObject o1 = new TestObject(4); - try { - CompareToBuilder.reflectionCompare(o1, null); - } catch (NullPointerException ex) { - return; - } - fail(); + CompareToBuilder.reflectionCompare(o1, null); } - @Test + @Test(expected=ClassCastException.class) public void testReflectionCompareEx2() { TestObject o1 = new TestObject(4); Object o2 = new Object(); - try { - CompareToBuilder.reflectionCompare(o1, o2); - fail(); - } catch (ClassCastException ex) {} + CompareToBuilder.reflectionCompare(o1, o2); } @Test @@ -295,14 +286,11 @@ public void testObjectBuild() { assertTrue(new CompareToBuilder().append(null, o1).build().intValue() < 0); } - @Test + @Test(expected=ClassCastException.class) public void testObjectEx2() { TestObject o1 = new TestObject(4); Object o2 = new Object(); - try { - new CompareToBuilder().append(o1, o2); - fail(); - } catch (ClassCastException ex) {} + new CompareToBuilder().append(o1, o2); } @Test diff --git a/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java index de57cab2d..e5d0820c1 100644 --- a/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java @@ -19,8 +19,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; -import static org.junit.Assert.fail; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -1014,15 +1012,12 @@ public void test_setUpToClass_valid() { /** * Tests ReflectionToStringBuilder setUpToClass(). */ - @Test + @Test(expected=IllegalArgumentException.class) public void test_setUpToClass_invalid() { Integer val = Integer.valueOf(5); ReflectionToStringBuilder test = new ReflectionToStringBuilder(val); try { test.setUpToClass(String.class); - fail(); - } catch (IllegalArgumentException ex) { - // expected } finally { test.toString(); } diff --git a/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java b/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java index 16c04252d..af80f2f93 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java @@ -17,7 +17,6 @@ package org.apache.commons.lang3.concurrent; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; @@ -38,14 +37,9 @@ public class CallableBackgroundInitializerTest { * Tries to create an instance without a Callable. This should cause an * exception. */ - @Test + @Test(expected=IllegalArgumentException.class) public void testInitNullCallable() { - try { - new CallableBackgroundInitializer(null); - fail("Could create instance without a Callable!"); - } catch (IllegalArgumentException iex) { - // ok - } + new CallableBackgroundInitializer(null); } /** @@ -64,15 +58,10 @@ public void testInitExecutor() { * Tries to pass a null Callable to the constructor that takes an executor. * This should cause an exception. */ - @Test + @Test(expected=IllegalArgumentException.class) public void testInitExecutorNullCallable() { ExecutorService exec = Executors.newSingleThreadExecutor(); - try { - new CallableBackgroundInitializer(null, exec); - fail("Could create instance without a Callable!"); - } catch (IllegalArgumentException iex) { - // ok - } + new CallableBackgroundInitializer(null, exec); } /**