Use assertThrows() in common-util 57/92557/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 21 Sep 2020 12:02:49 +0000 (14:02 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 21 Sep 2020 14:55:12 +0000 (14:55 +0000)
Using assertThrows() allows us to clean up some of the tests, so that
we do not have needed flow control and expected= tests.

Change-Id: I0b2ca1af3a50109ea68e0161c70f918b76275fb5
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/util/src/test/java/org/opendaylight/yangtools/util/EmptyDequeTest.java
common/util/src/test/java/org/opendaylight/yangtools/util/ImmutableMapTemplateTest.java
common/util/src/test/java/org/opendaylight/yangtools/util/OffsetMapTest.java
common/util/src/test/java/org/opendaylight/yangtools/util/SharedSingletonMapTest.java
common/util/src/test/java/org/opendaylight/yangtools/util/SingletonSetTest.java
common/util/src/test/java/org/opendaylight/yangtools/util/TopologicalSortTest.java
common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/CountingRejectedExecutionHandlerTest.java
common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/ReflectiveExceptionMapperTest.java
common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/TrackingLinkedBlockingQueueTest.java

index fafe79d7bacd38e8b4606f40e6dcd0ff0d288f49..b1d000426fca06adf8bdd085ebf0bd6edb93fb65 100644 (file)
@@ -11,7 +11,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertThrows;
 
 import java.util.NoSuchElementException;
 import org.junit.Test;
@@ -44,60 +44,14 @@ public class EmptyDequeTest {
         assertFalse(deque.removeFirstOccurrence(null));
         assertFalse(deque.removeLastOccurrence(null));
 
-        try {
-            deque.push(null);
-            fail();
-        } catch (IllegalStateException e) {
-            // expeced
-        }
-        try {
-            deque.addFirst(null);
-            fail();
-        } catch (IllegalStateException e) {
-            // expeced
-        }
-        try {
-            deque.addLast(null);
-            fail();
-        } catch (IllegalStateException e) {
-            // expeced
-        }
-
-        try {
-            deque.getFirst();
-            fail();
-        } catch (NoSuchElementException e) {
-            // expeced
-        }
-        try {
-            deque.getLast();
-            fail();
-        } catch (NoSuchElementException e) {
-            // expeced
-        }
-        try {
-            deque.pop();
-            fail();
-        } catch (NoSuchElementException e) {
-            // expeced
-        }
-        try {
-            deque.remove();
-            fail();
-        } catch (NoSuchElementException e) {
-            // expeced
-        }
-        try {
-            deque.removeFirst();
-            fail();
-        } catch (NoSuchElementException e) {
-            // expeced
-        }
-        try {
-            deque.removeLast();
-            fail();
-        } catch (NoSuchElementException e) {
-            // expeced
-        }
+        assertThrows(IllegalStateException.class, () -> deque.push(null));
+        assertThrows(IllegalStateException.class, () -> deque.addFirst(null));
+        assertThrows(IllegalStateException.class, () -> deque.addLast(null));
+        assertThrows(NoSuchElementException.class, () -> deque.getFirst());
+        assertThrows(NoSuchElementException.class, () -> deque.getLast());
+        assertThrows(NoSuchElementException.class, () -> deque.pop());
+        assertThrows(NoSuchElementException.class, () -> deque.remove());
+        assertThrows(NoSuchElementException.class, () -> deque.removeFirst());
+        assertThrows(NoSuchElementException.class, () -> deque.removeLast());
     }
 }
index 97caa8f105a4199a4f551890a919987f62c521eb..4e0814813abfd69bb0c00438c5ca486c46f577a9 100644 (file)
@@ -8,8 +8,8 @@
 package org.opendaylight.yangtools.util;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
@@ -27,20 +27,8 @@ public class ImmutableMapTemplateTest {
 
     @Test
     public void testEmpty() {
-        ImmutableMapTemplate<?> template;
-        try {
-            template = ImmutableMapTemplate.ordered(ImmutableList.of());
-            fail("Returned template " + template);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
-
-        try {
-            template = ImmutableMapTemplate.unordered(ImmutableList.of());
-            fail("Returned template " + template);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
+        assertThrows(IllegalArgumentException.class, () -> ImmutableMapTemplate.ordered(ImmutableList.of()));
+        assertThrows(IllegalArgumentException.class, () -> ImmutableMapTemplate.unordered(ImmutableList.of()));
     }
 
     @Test
@@ -71,48 +59,22 @@ public class ImmutableMapTemplateTest {
         assertEquals("{foo=foo}", map.toString());
 
         // Null transformation
-        try {
-            map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> null);
-            fail("Returned map " + map);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
+        assertThrows(IllegalArgumentException.class,
+            () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> null));
 
         // Empty input
-        try {
-            map = template.instantiateWithValues();
-            fail("Returned map " + map);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
-        try {
-            map = template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key);
-            fail("Returned map " + map);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
+        assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues());
+        assertThrows(IllegalArgumentException.class,
+            () -> template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key));
 
         // Two-item input
-        try {
-            map = template.instantiateWithValues(FOO, BAR);
-            fail("Returned map " + map);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
-        try {
-            map = template.instantiateTransformed(ImmutableMap.of(FOO, FOO, BAR, BAR), (key, value) -> key);
-            fail("Returned map " + map);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
+        assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues(FOO, BAR));
+        assertThrows(IllegalArgumentException.class,
+            () -> template.instantiateTransformed(ImmutableMap.of(FOO, FOO, BAR, BAR), (key, value) -> key));
 
         // Mismatched input
-        try {
-            map = template.instantiateTransformed(ImmutableMap.of(BAR, FOO), (key, value) -> key);
-            fail("Returned map " + map);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
+        assertThrows(IllegalArgumentException.class,
+            () -> template.instantiateTransformed(ImmutableMap.of(BAR, FOO), (key, value) -> key));
     }
 
     private static void assertTwo(final ImmutableMapTemplate<String> template, final Class<?> mapClass) {
@@ -131,39 +93,17 @@ public class ImmutableMapTemplateTest {
         assertEquals("{foo=foo, bar=bar}", map.toString());
 
         // Empty input
-        try {
-            map = template.instantiateWithValues();
-            fail("Returned map " + map);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
-        try {
-            map = template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key);
-            fail("Returned map " + map);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
+        assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues());
+        assertThrows(IllegalArgumentException.class,
+            () -> template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key));
 
         // One-item input
-        try {
-            map = template.instantiateWithValues(FOO);
-            fail("Returned map " + map);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
-        try {
-            map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> key);
-            fail("Returned map " + map);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
+        assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues(FOO));
+        assertThrows(IllegalArgumentException.class,
+            () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> key));
 
         // Mismatched input
-        try {
-            map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR, BAZ, FOO), (key, value) -> key);
-            fail("Returned map " + map);
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
+        assertThrows(IllegalArgumentException.class,
+            () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR, BAZ, FOO), (key, value) -> key));
     }
 }
index bc93d2cfde480884c1428444a720c582d17b9876..477316f72b5ef0479ad57a623acdc8f135117286 100644 (file)
@@ -12,8 +12,8 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterables;
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.util.AbstractMap.SimpleEntry;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.ConcurrentModificationException;
 import java.util.Iterator;
@@ -51,9 +52,9 @@ public class OffsetMapTest {
         OffsetMapCache.invalidateCache();
     }
 
-    @Test(expected = IllegalArgumentException.class)
     public void testWrongImmutableConstruction() {
-        new ImmutableOffsetMap.Ordered<>(ImmutableMap.of(), new String[1]);
+        assertThrows(IllegalArgumentException.class,
+            () -> new ImmutableOffsetMap.Ordered<>(ImmutableMap.of(), new String[1]));
     }
 
     @Test
@@ -123,123 +124,37 @@ public class OffsetMapTest {
     public void testImmutableGuards() {
         final Map<String, String> map = createMap();
 
-        try {
-            map.values().add("v1");
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
-
-        try {
-            map.values().remove("v1");
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
-
-        try {
-            map.values().clear();
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
-
-        try {
-            final Iterator<String> it = map.values().iterator();
-            it.next();
-            it.remove();
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
-
-        try {
-            map.keySet().add("k1");
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
-
-        try {
-            map.keySet().clear();
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
-
-        try {
-            map.keySet().remove("k1");
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
-
-        try {
-            final Iterator<String> it = map.keySet().iterator();
-            it.next();
-            it.remove();
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
-
-        try {
-            map.entrySet().clear();
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
-
-        try {
-            map.entrySet().add(new SimpleEntry<>("k1", "v1"));
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
+        final Collection<String> values = map.values();
+        assertThrows(UnsupportedOperationException.class, () -> values.add("v1"));
+        assertThrows(UnsupportedOperationException.class, () -> values.remove("v1"));
+        assertThrows(UnsupportedOperationException.class, () -> values.clear());
 
-        try {
-            map.entrySet().remove(new SimpleEntry<>("k1", "v1"));
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
+        final Iterator<String> vit = values.iterator();
+        vit.next();
+        assertThrows(UnsupportedOperationException.class, () -> vit.remove());
 
-        try {
-            final Iterator<Entry<String, String>> it = map.entrySet().iterator();
-            it.next();
-            it.remove();
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
+        final Set<String> keySet = map.keySet();
+        assertThrows(UnsupportedOperationException.class, () -> keySet.add("k1"));
+        assertThrows(UnsupportedOperationException.class, () -> keySet.clear());
+        assertThrows(UnsupportedOperationException.class, () -> keySet.remove("k1"));
 
-        try {
-            map.clear();
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
+        final Iterator<String> kit = keySet.iterator();
+        kit.next();
+        assertThrows(UnsupportedOperationException.class, () -> kit.remove());
 
-        try {
-            map.put("k1", "fail");
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
+        final Set<Entry<String, String>> entrySet = map.entrySet();
+        assertThrows(UnsupportedOperationException.class, () -> entrySet.clear());
+        assertThrows(UnsupportedOperationException.class, () -> entrySet.add(new SimpleEntry<>("k1", "v1")));
+        assertThrows(UnsupportedOperationException.class, () -> entrySet.remove(new SimpleEntry<>("k1", "v1")));
 
-        try {
-            map.putAll(ImmutableMap.of("k1", "fail"));
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
+        final Iterator<Entry<String, String>> eit = entrySet.iterator();
+        eit.next();
+        assertThrows(UnsupportedOperationException.class, () -> eit.remove());
 
-        try {
-            map.remove("k1");
-            fail();
-        } catch (UnsupportedOperationException e) {
-            // OK
-        }
+        assertThrows(UnsupportedOperationException.class, () -> map.clear());
+        assertThrows(UnsupportedOperationException.class, () -> map.put("k1", "fail"));
+        assertThrows(UnsupportedOperationException.class, () -> map.putAll(ImmutableMap.of("k1", "fail")));
+        assertThrows(UnsupportedOperationException.class, () -> map.remove("k1"));
     }
 
     @Test
@@ -552,8 +467,8 @@ public class OffsetMapTest {
         assertTrue(result.needClone());
 
         // Creates a immutable view, which shares the array
-        final ImmutableOffsetMap<String, String> immutable = (ImmutableOffsetMap<String, String>) source
-                .toUnmodifiableMap();
+        final ImmutableOffsetMap<String, String> immutable =
+                (ImmutableOffsetMap<String, String>) source.toUnmodifiableMap();
         assertTrue(source.needClone());
         assertSame(source.array(), immutable.objects());
     }
@@ -582,24 +497,9 @@ public class OffsetMapTest {
     }
 
     private static void assertIteratorBroken(final Iterator<?> it) {
-        try {
-            it.hasNext();
-            fail();
-        } catch (ConcurrentModificationException e) {
-            // OK
-        }
-        try {
-            it.next();
-            fail();
-        } catch (ConcurrentModificationException e) {
-            // OK
-        }
-        try {
-            it.remove();
-            fail();
-        } catch (ConcurrentModificationException e) {
-            // OK
-        }
+        assertThrows(ConcurrentModificationException.class, () -> it.hasNext());
+        assertThrows(ConcurrentModificationException.class, () -> it.next());
+        assertThrows(ConcurrentModificationException.class, () -> it.remove());
     }
 
     @Test
@@ -626,12 +526,7 @@ public class OffsetMapTest {
         final Iterator<Entry<String, String>> it = map.entrySet().iterator();
 
         // Not advanced, remove should fail
-        try {
-            it.remove();
-            fail();
-        } catch (IllegalStateException e) {
-            // OK
-        }
+        assertThrows(IllegalStateException.class, () -> it.remove());
 
         assertTrue(it.hasNext());
         assertEquals("k1", it.next().getKey());
@@ -640,12 +535,7 @@ public class OffsetMapTest {
         assertFalse(it.hasNext());
 
         // Check end-of-iteration throw
-        try {
-            it.next();
-            fail();
-        } catch (NoSuchElementException e) {
-            // OK
-        }
+        assertThrows(NoSuchElementException.class, () -> it.next());
     }
 
     @Test
index f47e859797be295d25be53c43fd4dd5e64c6d3d7..a8d8a461646134050191ec639ab393bc117918b2 100644 (file)
@@ -7,9 +7,12 @@
  */
 package org.opendaylight.yangtools.util;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import com.google.common.collect.ImmutableMap;
@@ -64,7 +67,7 @@ public class SharedSingletonMapTest {
         assertFalse(m.equals(ImmutableMap.of("k1", "v1", "k2", "v2")));
 
         final Set<String> set = m.keySet();
-        assertTrue(set instanceof SingletonSet);
+        assertThat(set, instanceOf(SingletonSet.class));
         assertTrue(set.contains("k1"));
     }
 
@@ -84,33 +87,33 @@ public class SharedSingletonMapTest {
         assertTrue(m.equals(t));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testEmptyOrderedCopyOf() {
-        SharedSingletonMap.orderedCopyOf(ImmutableMap.of());
+        assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.orderedCopyOf(ImmutableMap.of()));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testEmptyUnorderedCopyOf() {
-        SharedSingletonMap.unorderedCopyOf(ImmutableMap.of());
+        assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.unorderedCopyOf(ImmutableMap.of()));
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void testClear() {
-        create().clear();
+        assertThrows(UnsupportedOperationException.class, () -> create().clear());
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void testPut() {
-        create().put(null, null);
+        assertThrows(UnsupportedOperationException.class, () -> create().put(null, null));
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void testPutAll() {
-        create().putAll(Collections.singletonMap("", ""));
+        assertThrows(UnsupportedOperationException.class, () -> create().putAll(Collections.singletonMap("", "")));
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void testRemove() {
-        create().remove(null);
+        assertThrows(UnsupportedOperationException.class, () -> create().remove(null));
     }
 }
index 8438554104cdda233c5665d0ab590ddf278dd7a0..68a9ca954c1a59df6f822a17eecfff53babdfb14 100644 (file)
@@ -11,6 +11,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import java.util.Collections;
@@ -73,39 +74,39 @@ public class SingletonSetTest {
         assertFalse(it.hasNext());
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void testRejectedAdd() {
         final SingletonSet<?> s = nullSet();
-        s.add(null);
+        assertThrows(UnsupportedOperationException.class, () -> s.add(null));
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void testRejectedAddAll() {
         final SingletonSet<?> s = nullSet();
-        s.addAll(null);
+        assertThrows(UnsupportedOperationException.class, () -> s.addAll(null));
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void testRejectedClear() {
         final SingletonSet<?> s = nullSet();
-        s.clear();
+        assertThrows(UnsupportedOperationException.class, () -> s.clear());
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void testRejectedRemove() {
         final SingletonSet<?> s = nullSet();
-        s.remove(null);
+        assertThrows(UnsupportedOperationException.class, () -> s.remove(null));
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void testRejectedRemoveAll() {
         final SingletonSet<?> s = nullSet();
-        s.removeAll(null);
+        assertThrows(UnsupportedOperationException.class, () -> s.removeAll(null));
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void testRejectedRetainAll() {
         final SingletonSet<?> s = nullSet();
-        s.retainAll(null);
+        assertThrows(UnsupportedOperationException.class, () -> s.retainAll(null));
     }
 }
index 37043a1e62870c2f6faa35e903060d25d133970a..f59a96b1f88d3fd7b1eee8d18340c102da162bc5 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.util;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 
 import java.util.HashSet;
 import java.util.List;
@@ -18,7 +19,7 @@ import org.opendaylight.yangtools.util.TopologicalSort.NodeImpl;
 
 public class TopologicalSortTest {
 
-    @Test(expected = IllegalStateException.class)
+    @Test
     public void test() {
         Set<Node> nodes = new HashSet<>();
 
@@ -33,8 +34,7 @@ public class TopologicalSortTest {
         node2.addEdge(node3);
         node3.addEdge(node1);
 
-        // We expect an IllegalStateException here
-        TopologicalSort.sort(nodes);
+        assertThrows(IllegalStateException.class, () -> TopologicalSort.sort(nodes));
     }
 
     @Test
index e6dd9f3f5e024552228ec90e4f7e31b4b613ed49..d69565166c993074a0bd54511dd6e3b951650307 100644 (file)
@@ -5,12 +5,11 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.yangtools.util.concurrent;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.LinkedBlockingQueue;
@@ -80,12 +79,7 @@ public class CountingRejectedExecutionHandlerTest {
 
         int tasks = 5;
         for (int i = 0; i < tasks - 1; i++) {
-            try {
-                executor.execute(new Task(null, null, null, null, 0));
-                fail("Expected RejectedExecutionException");
-            } catch (RejectedExecutionException e) {
-                // Expected
-            }
+            assertThrows(RejectedExecutionException.class, () -> executor.execute(new Task(null, null, null, null, 0)));
         }
 
         assertEquals("getRejectedTaskCount", tasks - 1, countingHandler.getRejectedTaskCount());
index 2e1aa18c06671e6edc06f839c21e9aa97af79967..388b9ed8a6c4089bcd252d4922ad2c7e0572529b 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.util.concurrent;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 
 import java.util.concurrent.ExecutionException;
 import org.junit.Test;
@@ -45,19 +46,22 @@ public class ReflectiveExceptionMapperTest {
     }
 
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testNoArgumentsContructor() {
-        ReflectiveExceptionMapper.create("no arguments", NoArgumentCtorException.class);
+        assertThrows(IllegalArgumentException.class,
+            () -> ReflectiveExceptionMapper.create("no arguments", NoArgumentCtorException.class));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testPrivateContructor() {
-        ReflectiveExceptionMapper.create("private constructor", PrivateCtorException.class);
+        assertThrows(IllegalArgumentException.class,
+            () -> ReflectiveExceptionMapper.create("private constructor", PrivateCtorException.class));
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test
     public void testFailingContructor() {
-        ReflectiveExceptionMapper.create("failing constructor", FailingCtorException.class);
+        assertThrows(IllegalArgumentException.class,
+            () -> ReflectiveExceptionMapper.create("failing constructor", FailingCtorException.class));
     }
 
     @Test
index 17c66d7a247bf99d2a1685959998f835996ac2b1..cde62a771fe4c6a0f3aa62d80e6d063b5b7120a3 100644 (file)
@@ -5,13 +5,12 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.yangtools.util.concurrent;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import java.util.Arrays;
 import java.util.concurrent.TimeUnit;
@@ -70,14 +69,9 @@ public class TrackingLinkedBlockingQueueTest {
         assertEquals("getLargestQueueSize", 2, queue.getLargestQueueSize());
         assertEquals("size", 2, queue.size());
 
-        try {
-            queue.add("3");
-            fail("Expected IllegalStateException");
-        } catch (IllegalStateException e) {
-            // Expected
-            assertEquals("getLargestQueueSize", 2, queue.getLargestQueueSize());
-            assertEquals("size", 2, queue.size());
-        }
+        assertThrows(IllegalStateException.class, () -> queue.add("3"));
+        assertEquals("getLargestQueueSize", 2, queue.getLargestQueueSize());
+        assertEquals("size", 2, queue.size());
     }
 
     @Test
@@ -88,13 +82,8 @@ public class TrackingLinkedBlockingQueueTest {
         assertEquals("getLargestQueueSize", 2, queue.getLargestQueueSize());
         assertEquals("size", 2, queue.size());
 
-        try {
-            queue.addAll(Arrays.asList("3", "4"));
-            fail("Expected IllegalStateException");
-        } catch (IllegalStateException e) {
-            // Expected
-            assertEquals("getLargestQueueSize", 3, queue.getLargestQueueSize());
-            assertEquals("size", 3, queue.size());
-        }
+        assertThrows(IllegalStateException.class, () -> queue.addAll(Arrays.asList("3", "4")));
+        assertEquals("getLargestQueueSize", 3, queue.getLargestQueueSize());
+        assertEquals("size", 3, queue.size());
     }
 }