From 29ec97e7c85d607ba9e41cab3993fbb13f812c4b Mon Sep 17 00:00:00 2001 From: Tim van der Lippe Date: Tue, 9 Jul 2019 21:02:29 +0100 Subject: [PATCH] Migrate org.mockito.Matchers#any* to org.mockito.ArgumentMatchers (#402) The former is deprecated and replaced by the latter in Mockito 2. However, there is a functional difference: ArgumentMatchers will reject `null` and check the type if the matcher specified a type (e.g. `any(Class)` or `anyInt()`). `any()` will remain to accept anything. --- .../WebfilesValidatorProgramTest.java | 45 ++++++++++--------- .../webfiles/server/NetworkUtilsTest.java | 4 +- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/javatests/io/bazel/rules/closure/webfiles/WebfilesValidatorProgramTest.java b/javatests/io/bazel/rules/closure/webfiles/WebfilesValidatorProgramTest.java index 2881f63dd2..9683c1b2b4 100644 --- a/javatests/io/bazel/rules/closure/webfiles/WebfilesValidatorProgramTest.java +++ b/javatests/io/bazel/rules/closure/webfiles/WebfilesValidatorProgramTest.java @@ -16,7 +16,7 @@ import static com.google.common.truth.Truth.assertThat; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Matchers.contains; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.matches; @@ -41,7 +41,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.mockito.Mockito; +import org.mockito.ArgumentMatchers; /** Unit tests for {@link WebfilesValidatorProgram}. */ @RunWith(JUnit4.class) @@ -74,14 +74,15 @@ public void targetFlag_loadsProtoAndPassesAlong() throws Exception { save(fs.getPath("/a.pbtxt"), "label: \"@oh//my:goth\"\n"); when(validator.validate( any(Webfiles.class), - Mockito.>any(), - Mockito.>>any())) + ArgumentMatchers.>any(), + ArgumentMatchers.>>any())) .thenReturn(ArrayListMultimap.create()); assertThat(program.apply(ImmutableList.of("--target", "/a.pbtxt"))).isEqualTo(0); - verify(validator).validate( - eq(Webfiles.newBuilder().setLabel("@oh//my:goth").build()), - eq(ImmutableList.of()), - Mockito.>>any()); + verify(validator) + .validate( + eq(Webfiles.newBuilder().setLabel("@oh//my:goth").build()), + eq(ImmutableList.of()), + ArgumentMatchers.>>any()); } @Test @@ -89,14 +90,15 @@ public void validatorReturnsError_getsPrintedAndReturnsNonZeroWithProTip() throw save(fs.getPath("/a.pbtxt"), "label: \"@oh//my:goth\"\n"); when(validator.validate( any(Webfiles.class), - Mockito.>any(), - Mockito.>>any())) + ArgumentMatchers.>any(), + ArgumentMatchers.>>any())) .thenReturn(ArrayListMultimap.create(ImmutableMultimap.of("navi", "hey listen"))); assertThat(program.apply(ImmutableList.of("--target", "/a.pbtxt"))).isEqualTo(1); - verify(validator).validate( - eq(Webfiles.newBuilder().setLabel("@oh//my:goth").build()), - eq(ImmutableList.of()), - Mockito.>>any()); + verify(validator) + .validate( + eq(Webfiles.newBuilder().setLabel("@oh//my:goth").build()), + eq(ImmutableList.of()), + ArgumentMatchers.>>any()); verify(output).println(matches(".*ERROR.*hey listen")); verify(output).printf(matches(".*suppress.*"), matches(".*NOTE.*"), eq("navi")); } @@ -106,8 +108,8 @@ public void suppressCategory_errorBecomesWarningAndReturnsZeroWithoutProTip() th save(fs.getPath("/a.pbtxt"), "label: \"@oh//my:goth\"\n"); when(validator.validate( any(Webfiles.class), - Mockito.>any(), - Mockito.>>any())) + ArgumentMatchers.>any(), + ArgumentMatchers.>>any())) .thenReturn(ArrayListMultimap.create(ImmutableMultimap.of("navi", "hey listen"))); assertThat( program.apply( @@ -115,10 +117,11 @@ public void suppressCategory_errorBecomesWarningAndReturnsZeroWithoutProTip() th "--target", "/a.pbtxt", "--suppress", "navi"))) .isEqualTo(0); - verify(validator).validate( - eq(Webfiles.newBuilder().setLabel("@oh//my:goth").build()), - eq(ImmutableList.of()), - Mockito.>>any()); + verify(validator) + .validate( + eq(Webfiles.newBuilder().setLabel("@oh//my:goth").build()), + eq(ImmutableList.of()), + ArgumentMatchers.>>any()); verify(output).println(matches(".*WARNING.*hey listen")); } @@ -126,4 +129,4 @@ private void save(Path path, String contents) throws IOException { Files.createDirectories(path.getParent()); Files.write(path, contents.getBytes(UTF_8)); } -} +} \ No newline at end of file diff --git a/javatests/io/bazel/rules/closure/webfiles/server/NetworkUtilsTest.java b/javatests/io/bazel/rules/closure/webfiles/server/NetworkUtilsTest.java index 629f062847..84cc701c8b 100644 --- a/javatests/io/bazel/rules/closure/webfiles/server/NetworkUtilsTest.java +++ b/javatests/io/bazel/rules/closure/webfiles/server/NetworkUtilsTest.java @@ -14,8 +14,8 @@ package io.bazel.rules.closure.webfiles.server; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyInt; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when;