From 3eb81c8dac223b82c8ebc277e7646e4ea38054ec Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 23 Jan 2020 13:46:39 +0100 Subject: [PATCH] Migrate concepts tests to use assertThrows() Rather than using Test(expected=) use a dedicated test, which pinpoints exactly which call should throw the exception. Change-Id: I7ff2af221e3260b04ce9a31b7562f6e516af5fdc Signed-off-by: Robert Varga --- .../AbstractIllegalArgumentCodecTest.java | 10 ++++---- .../yangtools/concepts/CheckedValueTest.java | 24 ++++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/common/concepts/src/test/java/org/opendaylight/yangtools/concepts/AbstractIllegalArgumentCodecTest.java b/common/concepts/src/test/java/org/opendaylight/yangtools/concepts/AbstractIllegalArgumentCodecTest.java index da8b624fc7..fe11a9b484 100644 --- a/common/concepts/src/test/java/org/opendaylight/yangtools/concepts/AbstractIllegalArgumentCodecTest.java +++ b/common/concepts/src/test/java/org/opendaylight/yangtools/concepts/AbstractIllegalArgumentCodecTest.java @@ -7,6 +7,8 @@ */ package org.opendaylight.yangtools.concepts; +import static org.junit.Assert.assertThrows; + import org.junit.Test; public class AbstractIllegalArgumentCodecTest { @@ -24,13 +26,13 @@ public class AbstractIllegalArgumentCodecTest { private final TestCodec codec = new TestCodec(); - @Test(expected = NullPointerException.class) + @Test public void testNullDeserialize() { - codec.deserialize(null); + assertThrows(NullPointerException.class, () -> codec.deserialize(null)); } - @Test(expected = NullPointerException.class) + @Test public void testNullSerialize() { - codec.serialize(null); + assertThrows(NullPointerException.class, () -> codec.serialize(null)); } } diff --git a/common/concepts/src/test/java/org/opendaylight/yangtools/concepts/CheckedValueTest.java b/common/concepts/src/test/java/org/opendaylight/yangtools/concepts/CheckedValueTest.java index d39de3b110..449eb0bbea 100644 --- a/common/concepts/src/test/java/org/opendaylight/yangtools/concepts/CheckedValueTest.java +++ b/common/concepts/src/test/java/org/opendaylight/yangtools/concepts/CheckedValueTest.java @@ -10,6 +10,7 @@ package org.opendaylight.yangtools.concepts; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; @@ -24,19 +25,20 @@ import java.util.function.Supplier; import org.junit.Test; public class CheckedValueTest { - @Test(expected = NullPointerException.class) + @Test public void testNullValue() { - CheckedValue.ofValue(null); + assertThrows(NullPointerException.class, () -> CheckedValue.ofValue(null)); } - @Test(expected = IllegalStateException.class) public void testExceptionGet() { - CheckedValue.ofException(new Exception()).get(); + final CheckedValue value = CheckedValue.ofException(new Exception()); + assertThrows(IllegalStateException.class, () -> value.get()); } - @Test(expected = IllegalStateException.class) + @Test public void testValueException() { - CheckedValue.ofValue("foo").getException(); + final CheckedValue value = CheckedValue.ofValue("foo"); + assertThrows(IllegalStateException.class, () -> value.getException()); } @Test @@ -141,11 +143,11 @@ public class CheckedValueTest { .orElseThrow((Supplier)NullPointerException::new)); } - @Test(expected = InterruptedException.class) - public void testThrowableOrElseThrow() throws InterruptedException { - final String foo = "foo"; - final Exception cause = new NullPointerException(foo); - CheckedValue.ofException(cause).orElseThrow((Supplier)InterruptedException::new); + @Test + public void testThrowableOrElseThrow() { + final CheckedValue value = CheckedValue.ofException(new NullPointerException("foo")); + final Exception mock = mock(Exception.class); + assertThrows(mock.getClass(), () -> value.orElseThrow(() -> mock)); } @Test -- 2.36.6