Migrate testutils to JUnit5 74/104574/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 23 Feb 2023 19:38:10 +0000 (20:38 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 23 Feb 2023 19:38:49 +0000 (20:38 +0100)
This is mostly an automated conversion. Also moves tests into the same
package as the tested code.

Change-Id: I70c03c40dfef2aa466aa2c6ca723ccd8a502a9eb
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/MethodExtensionsTest.java [moved from common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MethodExtensionsTest.java with 66% similarity]
common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/MikitoTest.java [moved from common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MikitoTest.java with 62% similarity]
common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/MockitoExampleTutorialTest.java [moved from common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoExampleTutorialTest.java with 71% similarity]
common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/MockitoUnstubbedMethodExceptionAnswerTest.java [moved from common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoUnstubbedMethodExceptionAnswerTest.java with 53% similarity]

similarity index 66%
rename from common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MethodExtensionsTest.java
rename to common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/MethodExtensionsTest.java
index 9d423ca684bd9c52ef6277f31f10a2b3a4c7042c..e6fec26c47751f8f3ecc19937e8340a4c1dc62c1 100644 (file)
@@ -5,21 +5,20 @@
  * 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.testutils.mockito.tests;
+package org.opendaylight.yangtools.testutils.mockito;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import java.lang.reflect.Method;
-import org.junit.Test;
-import org.opendaylight.yangtools.testutils.mockito.MethodExtensions;
+import org.junit.jupiter.api.Test;
 
 public class MethodExtensionsTest {
-    public <T> void fooBar(int index, T element) {
+    public <T> void fooBar(final int index, final T element) {
         // No-op
     }
 
     @Test
-    public void betterToString() throws Exception {
+    void betterToString() throws Exception {
         Method method = MethodExtensionsTest.class.getMethod("fooBar", Integer.TYPE, Object.class);
         assertEquals("fooBar(int index, T element)", MethodExtensions.toString(method));
     }
similarity index 62%
rename from common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MikitoTest.java
rename to common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/MikitoTest.java
index 105a1b026c7023c565bd2cbd32c01e0ddd953bae..0fdf45fde0c9effb0390536c5a7f5709cb9b6607 100644 (file)
@@ -5,27 +5,24 @@
  * 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.testutils.mockito.tests;
+package org.opendaylight.yangtools.testutils.mockito;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.mock;
 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
 
 import java.io.File;
-import org.junit.Test;
-import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
+import org.junit.jupiter.api.Test;
 
 /**
- * Test to illustrate the use of the REAL_OR_EXCEPTION.
- *
- * <p>Also useful as example to contrast this approach illustrated in the MockitoExampleTutorialTest.
+ * Test to illustrate the use of the REAL_OR_EXCEPTION. Also useful as example to contrast this approach illustrated in
+ * the MockitoExampleTutorialTest.
  *
  * @see MockitoExampleTutorialTest
- *
  * @author Michael Vorburger
  */
-public class MikitoTest {
+class MikitoTest {
 
     interface SomeService {
 
@@ -38,17 +35,17 @@ public class MikitoTest {
     }
 
     @Test
-    public void usingMikitoToCallStubbedMethod() {
+    void usingMikitoToCallStubbedMethod() {
         SomeService service = mock(MockSomeService.class, realOrException());
         assertEquals(123, service.foobar(new File("hello.txt")));
         assertEquals(0, service.foobar(new File("belo.txt")));
     }
 
     @Test
-    public void usingMikitoToCallUnstubbedMethodAndExpectException() {
-        MockSomeService service = mock(MockSomeService.class, realOrException());
-        String message = assertThrows(UnstubbedMethodException.class, service::foo).getMessage();
-        assertEquals("foo() is not implemented in mockSomeService", message);
+    void usingMikitoToCallUnstubbedMethodAndExpectException() {
+        var service = mock(MockSomeService.class, realOrException());
+        var ex = assertThrows(UnstubbedMethodException.class, service::foo);
+        assertEquals("foo() is not implemented in mockSomeService", ex.getMessage());
     }
 
     abstract static class MockSomeService implements SomeService {
similarity index 71%
rename from common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoExampleTutorialTest.java
rename to common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/MockitoExampleTutorialTest.java
index f3faaaa11452ba5b8122bc81c10aa19d0d2265ec..78466ba100fe6c90b22ba7464c1fb7e2e7bf4558 100644 (file)
@@ -5,11 +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.testutils.mockito.tests;
+package org.opendaylight.yangtools.testutils.mockito;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doReturn;
@@ -18,20 +18,16 @@ import static org.mockito.Mockito.when;
 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.exception;
 
 import java.io.File;
-import org.junit.Test;
-import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
+import org.junit.jupiter.api.Test;
 
 /**
- * Test to illustrate the basic use of Mockito VS the EXCEPTION_ANSWER.
- *
- * <p>Also useful as example to contrast this approach with the REAL_OR_EXCEPTION
- * approach illustrated in the MikitoTest.
+ * Test to illustrate the basic use of Mockito VS the EXCEPTION_ANSWER. Also useful as example to contrast this approach
+ * with the REAL_OR_EXCEPTION approach illustrated in the MikitoTest.
  *
  * @see MikitoTest
- *
  * @author Michael Vorburger
  */
-public class MockitoExampleTutorialTest {
+class MockitoExampleTutorialTest {
 
     interface SomeService {
 
@@ -44,20 +40,20 @@ public class MockitoExampleTutorialTest {
     }
 
     @Test
-    public void usingMockitoWithoutStubbing() {
+    void usingMockitoWithoutStubbing() {
         SomeService service = mock(SomeService.class);
         assertNull(service.bar("hulo"));
     }
 
     @Test
-    public void usingMockitoToStubSimpleCase() {
+    void usingMockitoToStubSimpleCase() {
         SomeService service = mock(SomeService.class);
         when(service.foobar(any())).thenReturn(123);
         assertEquals(123, service.foobar(new File("hello.txt")));
     }
 
     @Test
-    public void usingMockitoToStubComplexCase() {
+    void usingMockitoToStubComplexCase() {
         SomeService service = mock(SomeService.class);
         when(service.foobar(any())).thenAnswer(invocation -> {
             File file = invocation.getArgument(0);
@@ -66,14 +62,16 @@ public class MockitoExampleTutorialTest {
         assertEquals(0, service.foobar(new File("belo.txt")));
     }
 
-    @Test(expected = UnstubbedMethodException.class)
-    public void usingMockitoExceptionException() {
-        SomeService service = mock(SomeService.class, exception());
-        service.foo();
+    @Test
+    void usingMockitoExceptionException() {
+        assertThrows(UnstubbedMethodException.class, () -> {
+            SomeService service = mock(SomeService.class, exception());
+            service.foo();
+        });
     }
 
     @Test
-    public void usingMockitoNoExceptionIfStubbed() {
+    void usingMockitoNoExceptionIfStubbed() {
         SomeService service = mock(SomeService.class, exception());
         // NOT when(s.foobar(any())).thenReturn(123) BUT must be like this:
         doReturn(123).when(service).foobar(any());
@@ -82,7 +80,7 @@ public class MockitoExampleTutorialTest {
     }
 
     @Test
-    public void usingMockitoToStubComplexCaseAndExceptionIfNotStubbed() {
+    void usingMockitoToStubComplexCaseAndExceptionIfNotStubbed() {
         SomeService service = mock(SomeService.class, exception());
         doAnswer(invocation -> {
             File file = invocation.getArgument(0);
@@ -5,22 +5,20 @@
  * 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.testutils.mockito.tests;
+package org.opendaylight.yangtools.testutils.mockito;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThrows;
-import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.exception;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.Closeable;
 import java.io.IOException;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;
-import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
 
-public class MockitoUnstubbedMethodExceptionAnswerTest {
+class MockitoUnstubbedMethodExceptionAnswerTest {
     @Test
-    public void testAnswering() throws IOException {
-        Closeable mock = Mockito.mock(Closeable.class, exception());
+    void testAnswering() throws IOException {
+        Closeable mock = Mockito.mock(Closeable.class, MoreAnswers.exception());
         String message = assertThrows(UnstubbedMethodException.class, mock::close).getMessage();
         assertEquals("close() is not stubbed in mock of java.io.Closeable", message);
     }