Skip to content

Commit

Permalink
mockito's "any" matcher no longer accepts null values
Browse files Browse the repository at this point in the history
  • Loading branch information
Kellindil committed Dec 28, 2022
1 parent fd182f3 commit b72bb6f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Expand Up @@ -18,6 +18,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -236,7 +237,7 @@ public void testExistsCache() throws CoreException {
when(nullStorageProvider.getStorage(any(IProgressMonitor.class))).thenReturn(null);

IStorageProviderAccessor accessor = mock(IStorageProviderAccessor.class);
when(accessor.getStorageProvider(any(IResource.class), any(DiffSide.class)))
when(accessor.getStorageProvider(nullable(IResource.class), any(DiffSide.class)))
.then(new Answer<IStorageProvider>() {
public IStorageProvider answer(InvocationOnMock invocation) throws Throwable {
return iFile1.equals(invocation.getArguments()[0]) ? fileStorageProvider
Expand Down
Expand Up @@ -13,6 +13,7 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -56,8 +57,9 @@ public void testCaching() {

// mock calculation
IImplicitDependencies delegate = mock(IImplicitDependencies.class);
when(delegate.of(any(URI.class), any(URIConverter.class))).then(new Answer<Set<URI>>() {
when(delegate.of(any(URI.class), nullable(URIConverter.class))).then(new Answer<Set<URI>>() {
public Set<URI> answer(InvocationOnMock invocation) throws Throwable {
System.out.println("hey!");
Object uriArgument = invocation.getArguments()[0];
if (uriArgument.equals(uri1)) {
return Sets.newHashSet(uri1dependencies);
Expand All @@ -75,8 +77,8 @@ public Set<URI> answer(InvocationOnMock invocation) throws Throwable {
assertEquals(uri2dependencies, cachingDependencies.of(uri2, null));

// delegate was only called once for each uri
verify(delegate, times(1)).of(eq(uri1), any(URIConverter.class));
verify(delegate, times(1)).of(eq(uri2), any(URIConverter.class));
verify(delegate, times(1)).of(eq(uri1), nullable(URIConverter.class));
verify(delegate, times(1)).of(eq(uri2), nullable(URIConverter.class));

// query dependencies multiple times
assertEquals(uri1dependencies, cachingDependencies.of(uri1, null));
Expand All @@ -89,7 +91,7 @@ public Set<URI> answer(InvocationOnMock invocation) throws Throwable {
assertEquals(uri2dependencies, cachingDependencies.of(uri2, null));

// delegate was only called once for each uri, cache used
verify(delegate, times(1)).of(eq(uri1), any(URIConverter.class));
verify(delegate, times(1)).of(eq(uri2), any(URIConverter.class));
verify(delegate, times(1)).of(eq(uri1), nullable(URIConverter.class));
verify(delegate, times(1)).of(eq(uri2), nullable(URIConverter.class));
}
}

0 comments on commit b72bb6f

Please sign in to comment.