Skip to content

Commit

Permalink
Migrate org.mockito.Matchers#any* to org.mockito.ArgumentMatchers (#402)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TimvdLippe authored and dslomov committed Jul 9, 2019
1 parent e7a3bcc commit 29ec97e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -74,29 +74,31 @@ public void targetFlag_loadsProtoAndPassesAlong() throws Exception {
save(fs.getPath("/a.pbtxt"), "label: \"@oh//my:goth\"\n");
when(validator.validate(
any(Webfiles.class),
Mockito.<Iterable<Webfiles>>any(),
Mockito.<Supplier<Iterable<Webfiles>>>any()))
ArgumentMatchers.<Iterable<Webfiles>>any(),
ArgumentMatchers.<Supplier<Iterable<Webfiles>>>any()))
.thenReturn(ArrayListMultimap.<String, String>create());
assertThat(program.apply(ImmutableList.of("--target", "/a.pbtxt"))).isEqualTo(0);
verify(validator).validate(
eq(Webfiles.newBuilder().setLabel("@oh//my:goth").build()),
eq(ImmutableList.<Webfiles>of()),
Mockito.<Supplier<Iterable<Webfiles>>>any());
verify(validator)
.validate(
eq(Webfiles.newBuilder().setLabel("@oh//my:goth").build()),
eq(ImmutableList.<Webfiles>of()),
ArgumentMatchers.<Supplier<Iterable<Webfiles>>>any());
}

@Test
public void validatorReturnsError_getsPrintedAndReturnsNonZeroWithProTip() throws Exception {
save(fs.getPath("/a.pbtxt"), "label: \"@oh//my:goth\"\n");
when(validator.validate(
any(Webfiles.class),
Mockito.<Iterable<Webfiles>>any(),
Mockito.<Supplier<Iterable<Webfiles>>>any()))
ArgumentMatchers.<Iterable<Webfiles>>any(),
ArgumentMatchers.<Supplier<Iterable<Webfiles>>>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.<Webfiles>of()),
Mockito.<Supplier<Iterable<Webfiles>>>any());
verify(validator)
.validate(
eq(Webfiles.newBuilder().setLabel("@oh//my:goth").build()),
eq(ImmutableList.<Webfiles>of()),
ArgumentMatchers.<Supplier<Iterable<Webfiles>>>any());
verify(output).println(matches(".*ERROR.*hey listen"));
verify(output).printf(matches(".*suppress.*"), matches(".*NOTE.*"), eq("navi"));
}
Expand All @@ -106,24 +108,25 @@ public void suppressCategory_errorBecomesWarningAndReturnsZeroWithoutProTip() th
save(fs.getPath("/a.pbtxt"), "label: \"@oh//my:goth\"\n");
when(validator.validate(
any(Webfiles.class),
Mockito.<Iterable<Webfiles>>any(),
Mockito.<Supplier<Iterable<Webfiles>>>any()))
ArgumentMatchers.<Iterable<Webfiles>>any(),
ArgumentMatchers.<Supplier<Iterable<Webfiles>>>any()))
.thenReturn(ArrayListMultimap.create(ImmutableMultimap.of("navi", "hey listen")));
assertThat(
program.apply(
ImmutableList.of(
"--target", "/a.pbtxt",
"--suppress", "navi")))
.isEqualTo(0);
verify(validator).validate(
eq(Webfiles.newBuilder().setLabel("@oh//my:goth").build()),
eq(ImmutableList.<Webfiles>of()),
Mockito.<Supplier<Iterable<Webfiles>>>any());
verify(validator)
.validate(
eq(Webfiles.newBuilder().setLabel("@oh//my:goth").build()),
eq(ImmutableList.<Webfiles>of()),
ArgumentMatchers.<Supplier<Iterable<Webfiles>>>any());
verify(output).println(matches(".*WARNING.*hey listen"));
}

private void save(Path path, String contents) throws IOException {
Files.createDirectories(path.getParent());
Files.write(path, contents.getBytes(UTF_8));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 29ec97e

Please sign in to comment.