From 80197ad7e563032a0243f4df9d178909c6f79610 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 28 Apr 2022 18:44:50 +0200 Subject: [PATCH 1/1] Modernize testutils tests Ditch the use of Truth and use assertThrows() instead of explicit try/catch blocks. Change-Id: I449b187fe5fa1497fe5bf6a63367e42fa9706fa7 Signed-off-by: Robert Varga --- common/testutils/pom.xml | 5 ----- .../mockito/tests/MethodExtensionsTest.java | 7 +++---- .../testutils/mockito/tests/MikitoTest.java | 11 +++-------- .../mockito/tests/MockitoExampleTutorialTest.java | 10 ++-------- ...MockitoUnstubbedMethodExceptionAnswerTest.java | 15 ++++----------- 5 files changed, 12 insertions(+), 36 deletions(-) diff --git a/common/testutils/pom.xml b/common/testutils/pom.xml index 0b5d830ed3..e82e071de4 100644 --- a/common/testutils/pom.xml +++ b/common/testutils/pom.xml @@ -51,14 +51,9 @@ mockito-core compile - - com.google.truth - truth - com.google.guava guava - diff --git a/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MethodExtensionsTest.java b/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MethodExtensionsTest.java index a48538ca98..9d423ca684 100644 --- a/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MethodExtensionsTest.java +++ b/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MethodExtensionsTest.java @@ -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 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)); } - } diff --git a/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MikitoTest.java b/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MikitoTest.java index 346dd3c1d4..105a1b026c 100644 --- a/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MikitoTest.java +++ b/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MikitoTest.java @@ -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 { diff --git a/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoExampleTutorialTest.java b/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoExampleTutorialTest.java index aad4655bcf..f3faaaa114 100644 --- a/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoExampleTutorialTest.java +++ b/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoExampleTutorialTest.java @@ -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"))); } - } diff --git a/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoUnstubbedMethodExceptionAnswerTest.java b/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoUnstubbedMethodExceptionAnswerTest.java index 2b1296f663..5dd9bff9cf 100644 --- a/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoUnstubbedMethodExceptionAnswerTest.java +++ b/common/testutils/src/test/java/org/opendaylight/yangtools/testutils/mockito/tests/MockitoUnstubbedMethodExceptionAnswerTest.java @@ -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); } - } -- 2.36.6