Modernize testutils tests 70/100870/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 28 Apr 2022 16:44:50 +0000 (18:44 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 28 Apr 2022 16:44:50 +0000 (18:44 +0200)
Ditch the use of Truth and use assertThrows() instead of explicit
try/catch blocks.

Change-Id: I449b187fe5fa1497fe5bf6a63367e42fa9706fa7
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/testutils/pom.xml
common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MethodExtensionsTest.java
common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MikitoTest.java
common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoExampleTutorialTest.java
common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoUnstubbedMethodExceptionAnswerTest.java

index 0b5d830ed31bc45a4daab4c77763d6fd6fad387e..e82e071de4d760c58fe0eb23522fa3dd6a8c3f67 100644 (file)
             <artifactId>mockito-core</artifactId>
             <scope>compile</scope>
         </dependency>
-        <dependency>
-            <groupId>com.google.truth</groupId>
-            <artifactId>truth</artifactId>
-        </dependency>
         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
         </dependency>
     </dependencies>
-
 </project>
index a48538ca98e7e7d58c54f383f0fe3f9c2421499d..9d423ca684bd9c52ef6277f31f10a2b3a4c7042c 100644 (file)
@@ -7,21 +7,20 @@
  */
 package org.opendaylight.yangtools.testutils.mockito.tests;
 
-import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertEquals;
 
 import java.lang.reflect.Method;
 import org.junit.Test;
 import org.opendaylight.yangtools.testutils.mockito.MethodExtensions;
 
 public class MethodExtensionsTest {
-
     public <T> void fooBar(int index, T element) {
+        // No-op
     }
 
     @Test
     public void betterToString() throws Exception {
         Method method = MethodExtensionsTest.class.getMethod("fooBar", Integer.TYPE, Object.class);
-        assertThat(MethodExtensions.toString(method)).isEqualTo("fooBar(int index, T element)");
+        assertEquals("fooBar(int index, T element)", MethodExtensions.toString(method));
     }
-
 }
index 346dd3c1d4abcb320147690ce45a18ba46e39287..105a1b026c7023c565bd2cbd32c01e0ddd953bae 100644 (file)
@@ -7,9 +7,8 @@
  */
 package org.opendaylight.yangtools.testutils.mockito.tests;
 
-import static com.google.common.truth.Truth.assertThat;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertThrows;
 import static org.mockito.Mockito.mock;
 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
 
@@ -48,12 +47,8 @@ public class MikitoTest {
     @Test
     public void usingMikitoToCallUnstubbedMethodAndExpectException() {
         MockSomeService service = mock(MockSomeService.class, realOrException());
-        try {
-            service.foo();
-            fail();
-        } catch (UnstubbedMethodException e) {
-            assertThat(e.getMessage()).isEqualTo("foo() is not implemented in mockSomeService");
-        }
+        String message = assertThrows(UnstubbedMethodException.class, service::foo).getMessage();
+        assertEquals("foo() is not implemented in mockSomeService", message);
     }
 
     abstract static class MockSomeService implements SomeService {
index aad4655bcf0e3cf1c5681dae20a95622030539fb..f3faaaa11452ba5b8122bc81c10aa19d0d2265ec 100644 (file)
@@ -9,7 +9,7 @@ package org.opendaylight.yangtools.testutils.mockito.tests;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertThrows;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doReturn;
@@ -78,12 +78,7 @@ public class MockitoExampleTutorialTest {
         // NOT when(s.foobar(any())).thenReturn(123) BUT must be like this:
         doReturn(123).when(service).foobar(any());
         assertEquals(123, service.foobar(new File("hello.txt")));
-        try {
-            service.foo();
-            fail("expected NotImplementedException");
-        } catch (UnstubbedMethodException e) {
-            // OK
-        }
+        assertThrows(UnstubbedMethodException.class, service::foo);
     }
 
     @Test
@@ -96,5 +91,4 @@ public class MockitoExampleTutorialTest {
         assertEquals(123, service.foobar(new File("hello.txt")));
         assertEquals(0, service.foobar(new File("belo.txt")));
     }
-
 }
index 2b1296f663cbac3f4dcec7c0af817314df2abe98..5dd9bff9cf6740675c92d93bdf6015f34d578caa 100644 (file)
@@ -7,8 +7,8 @@
  */
 package org.opendaylight.yangtools.testutils.mockito.tests;
 
-import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.exception;
 
 import java.io.Closeable;
@@ -18,17 +18,10 @@ import org.mockito.Mockito;
 import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
 
 public class MockitoUnstubbedMethodExceptionAnswerTest {
-
     @Test
     public void testAnswering() throws IOException {
         Closeable mock = Mockito.mock(Closeable.class, exception());
-        try {
-            mock.close();
-            fail();
-        } catch (UnstubbedMethodException e) {
-            assertThat(e.getMessage()).isEqualTo("close() is not stubbed in mock of java.io.Closeable");
-        }
-
+        String message = assertThrows(UnstubbedMethodException.class, mock::close).getMessage();
+        assertEquals("close() is not stubbed in mock of java.io.Closeable", message);
     }
-
 }