Migrate concepts tests to use assertThrows() 04/87104/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 23 Jan 2020 12:46:39 +0000 (13:46 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 23 Jan 2020 12:53:43 +0000 (13:53 +0100)
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 <robert.varga@pantheon.tech>
common/concepts/src/test/java/org/opendaylight/yangtools/concepts/AbstractIllegalArgumentCodecTest.java
common/concepts/src/test/java/org/opendaylight/yangtools/concepts/CheckedValueTest.java

index da8b624fc7d620f84cff2e6a2ce9b44d246790ef..fe11a9b4843fa3487ef0d752ea85a49c7e7e65de 100644 (file)
@@ -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));
     }
 }
index d39de3b110280dabc1d19a58190f4be793c37e53..449eb0bbea9e0ab36b244f4a3a8f5008cf90be90 100644 (file)
@@ -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>)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>)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