commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject [1/2] [lang] EventUtilsTest ExpectedException usage
Date Sun, 22 Oct 2017 09:08:36 GMT
Repository: commons-lang
Updated Branches:
  refs/heads/master 6276d0f84 -> 4eb1d6a44


EventUtilsTest ExpectedException usage

Use the ExpectedException @Rule to verify thrown exception instead of
boiler-plate implementing its logic, in order to clean up the code
and make it easier to read and maintain.


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/95fce758
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/95fce758
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/95fce758

Branch: refs/heads/master
Commit: 95fce758b0e12837869cae450d8029d7237b0dbb
Parents: 6276d0f
Author: Allon Mureinik <amureini@redhat.com>
Authored: Sat Oct 21 13:29:06 2017 +0300
Committer: Allon Mureinik <amureini@redhat.com>
Committed: Sat Oct 21 22:01:49 2017 +0300

----------------------------------------------------------------------
 .../commons/lang3/event/EventUtilsTest.java     | 42 +++++++++-----------
 1 file changed, 18 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/95fce758/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java
index 9c4265a..1b3f525 100644
--- a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java
@@ -20,7 +20,6 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
@@ -36,13 +35,18 @@ import java.util.TreeMap;
 
 import javax.naming.event.ObjectChangeListener;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 /**
  * @since 3.0
  */
 public class EventUtilsTest {
 
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
     @Test
     public void testConstructor() {
         assertNotNull(new EventUtils());
@@ -70,28 +74,21 @@ public class EventUtilsTest {
         final PropertyChangeSource src = new PropertyChangeSource();
         final EventCountingInvociationHandler handler = new EventCountingInvociationHandler();
         final ObjectChangeListener listener = handler.createListener(ObjectChangeListener.class);
-        try {
-            EventUtils.addEventListener(src, ObjectChangeListener.class, listener);
-            fail("Should not be allowed to add a listener to an object that doesn't support
it.");
-        } catch (final IllegalArgumentException e) {
-            assertEquals("Class " + src.getClass().getName() + " does not have a public add"
+ ObjectChangeListener.class.getSimpleName() + " method which takes a parameter of type "
+ ObjectChangeListener.class.getName() + ".", e.getMessage());
-        }
+        expectedException.expect(IllegalArgumentException.class);
+        expectedException.expectMessage("Class " + src.getClass().getName() + " does not
have a public add" + ObjectChangeListener.class.getSimpleName() + " method which takes a parameter
of type " + ObjectChangeListener.class.getName() + ".");
+        EventUtils.addEventListener(src, ObjectChangeListener.class, listener);
     }
 
     @Test
     public void testAddEventListenerThrowsException() {
         final ExceptionEventSource src = new ExceptionEventSource();
-        try {
-            EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener()
{
-                @Override
-                public void propertyChange(final PropertyChangeEvent e) {
-                    // Do nothing!
-                }
-            });
-            fail("Add method should have thrown an exception, so method should fail.");
-        } catch (final RuntimeException e) {
-
-        }
+        expectedException.expect(RuntimeException.class);
+        EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener()
{
+            @Override
+            public void propertyChange(final PropertyChangeEvent e) {
+                // Do nothing!
+            }
+        });
     }
 
     @Test
@@ -99,12 +96,9 @@ public class EventUtilsTest {
         final PropertyChangeSource src = new PropertyChangeSource();
         final EventCountingInvociationHandler handler = new EventCountingInvociationHandler();
         final VetoableChangeListener listener = handler.createListener(VetoableChangeListener.class);
-        try {
-            EventUtils.addEventListener(src, VetoableChangeListener.class, listener);
-            fail("Should not be allowed to add a listener to an object that doesn't support
it.");
-        } catch (final IllegalArgumentException e) {
-            assertEquals("Class " + src.getClass().getName() + " does not have a public add"
+ VetoableChangeListener.class.getSimpleName() + " method which takes a parameter of type
" + VetoableChangeListener.class.getName() + ".", e.getMessage());
-        }
+        expectedException.expect(IllegalArgumentException.class);
+        expectedException.expectMessage("Class " + src.getClass().getName() + " does not
have a public add" + VetoableChangeListener.class.getSimpleName() + " method which takes a
parameter of type " + VetoableChangeListener.class.getName() + ".");
+        EventUtils.addEventListener(src, VetoableChangeListener.class, listener);
     }
 
     @Test


Mime
View raw message