From 762641dcdbae9456aa2b72ec8fa1baa0acab942f Mon Sep 17 00:00:00 2001 From: Allon Mureinik Date: Tue, 2 Oct 2018 06:41:37 +0300 Subject: [PATCH] Update event tests to JUnit Jupiter Upgrade the tests in the event package to use JUnit Jupiter as part of the effort to remove the dependency on the Vintage Engine. While most of these changes are drop-in replacements with no functional benefit, it is worth noting the tests that test thrown excetpions. Prior to this patch, this package tested for exceptions in two ways, neither of which are supported in JUnit Jupiter: 1. With the "expected" argument of the org.junit.Test annotation. 2. With the org.junit.rules.ExpectedException @Rule Both of these usages were replaced with calls to org.junit.jupiter.api.Assertions#assertThrows. --- .../lang3/event/EventListenerSupportTest.java | 23 +++++----- .../commons/lang3/event/EventUtilsTest.java | 46 +++++++++---------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java index ffa78d684..a62b3da9c 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java @@ -17,8 +17,9 @@ package org.apache.commons.lang3.event; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.beans.PropertyChangeEvent; import java.beans.PropertyVetoException; @@ -34,7 +35,7 @@ import java.util.List; import org.easymock.EasyMock; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * @since 3.0 @@ -61,16 +62,16 @@ public void testAddListenerNoDuplicates() { assertSame(empty, listenerSupport.getListeners()); } - @Test(expected = NullPointerException.class) + @Test public void testAddNullListener() { final EventListenerSupport listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); - listenerSupport.addListener(null); + assertThrows(NullPointerException.class, () -> listenerSupport.addListener(null)); } - @Test(expected = NullPointerException.class) + @Test public void testRemoveNullListener() { final EventListenerSupport listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); - listenerSupport.removeListener(null); + assertThrows(NullPointerException.class, () -> listenerSupport.removeListener(null)); } @Test @@ -88,14 +89,14 @@ public void testEventDispatchOrder() throws PropertyVetoException { assertSame(calledListeners.get(1), listener2); } - @Test(expected = IllegalArgumentException.class) + @Test public void testCreateWithNonInterfaceParameter() { - EventListenerSupport.create(String.class); + assertThrows(IllegalArgumentException.class, () -> EventListenerSupport.create(String.class)); } - @Test(expected = NullPointerException.class) + @Test public void testCreateWithNullParameter() { - EventListenerSupport.create(null); + assertThrows(NullPointerException.class, () -> EventListenerSupport.create(null)); } @Test 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 1b3f525ca..030fca112 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java @@ -16,10 +16,11 @@ */ package org.apache.commons.lang3.event; -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.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -35,18 +36,12 @@ import javax.naming.event.ObjectChangeListener; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; /** * @since 3.0 */ public class EventUtilsTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Test public void testConstructor() { assertNotNull(new EventUtils()); @@ -74,21 +69,23 @@ public void testAddEventListenerWithNoAddMethod() { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvociationHandler handler = new EventCountingInvociationHandler(); final ObjectChangeListener listener = handler.createListener(ObjectChangeListener.class); - 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); + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> EventUtils.addEventListener(src, ObjectChangeListener.class, listener)); + 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()); } @Test public void testAddEventListenerThrowsException() { final ExceptionEventSource src = new ExceptionEventSource(); - expectedException.expect(RuntimeException.class); - EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener() { - @Override - public void propertyChange(final PropertyChangeEvent e) { - // Do nothing! - } - }); + assertThrows(RuntimeException.class, () -> + EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener() { + @Override + public void propertyChange(final PropertyChangeEvent e) { + // Do nothing! + } + }) + ); } @Test @@ -96,9 +93,10 @@ public void testAddEventListenerWithPrivateAddMethod() { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvociationHandler handler = new EventCountingInvociationHandler(); final VetoableChangeListener listener = handler.createListener(VetoableChangeListener.class); - 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); + IllegalArgumentException e = + assertThrows(IllegalArgumentException.class, () -> EventUtils.addEventListener(src, VetoableChangeListener.class, listener)); + 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()); } @Test