From: Robert Varga Date: Mon, 21 Sep 2020 12:02:49 +0000 (+0200) Subject: Use assertThrows() in common-util X-Git-Tag: v6.0.0~52 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=1346d7f11d7fa4cc392592e30fcb81b7afde56e1;p=yangtools.git Use assertThrows() in common-util 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 --- diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/EmptyDequeTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/EmptyDequeTest.java index fafe79d7ba..b1d000426f 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/EmptyDequeTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/EmptyDequeTest.java @@ -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()); } } diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/ImmutableMapTemplateTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/ImmutableMapTemplateTest.java index 97caa8f105..4e0814813a 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/ImmutableMapTemplateTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/ImmutableMapTemplateTest.java @@ -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 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)); } } diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/OffsetMapTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/OffsetMapTest.java index bc93d2cfde..477316f72b 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/OffsetMapTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/OffsetMapTest.java @@ -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 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 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 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 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 vit = values.iterator(); + vit.next(); + assertThrows(UnsupportedOperationException.class, () -> vit.remove()); - try { - final Iterator> it = map.entrySet().iterator(); - it.next(); - it.remove(); - fail(); - } catch (UnsupportedOperationException e) { - // OK - } + final Set 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 kit = keySet.iterator(); + kit.next(); + assertThrows(UnsupportedOperationException.class, () -> kit.remove()); - try { - map.put("k1", "fail"); - fail(); - } catch (UnsupportedOperationException e) { - // OK - } + final Set> 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> 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 immutable = (ImmutableOffsetMap) source - .toUnmodifiableMap(); + final ImmutableOffsetMap immutable = + (ImmutableOffsetMap) 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> 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 diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/SharedSingletonMapTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/SharedSingletonMapTest.java index f47e859797..a8d8a46164 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/SharedSingletonMapTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/SharedSingletonMapTest.java @@ -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 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)); } } diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/SingletonSetTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/SingletonSetTest.java index 8438554104..68a9ca954c 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/SingletonSetTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/SingletonSetTest.java @@ -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)); } } diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/TopologicalSortTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/TopologicalSortTest.java index 37043a1e62..f59a96b1f8 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/TopologicalSortTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/TopologicalSortTest.java @@ -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 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 diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/CountingRejectedExecutionHandlerTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/CountingRejectedExecutionHandlerTest.java index e6dd9f3f5e..d69565166c 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/CountingRejectedExecutionHandlerTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/CountingRejectedExecutionHandlerTest.java @@ -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()); diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/ReflectiveExceptionMapperTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/ReflectiveExceptionMapperTest.java index 2e1aa18c06..388b9ed8a6 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/ReflectiveExceptionMapperTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/ReflectiveExceptionMapperTest.java @@ -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 diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/TrackingLinkedBlockingQueueTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/TrackingLinkedBlockingQueueTest.java index 17c66d7a24..cde62a771f 100644 --- a/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/TrackingLinkedBlockingQueueTest.java +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/concurrent/TrackingLinkedBlockingQueueTest.java @@ -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()); } }