Skip to content

Latest commit

 

History

History
203 lines (146 loc) · 9.08 KB

migration_aids_v4.md

File metadata and controls

203 lines (146 loc) · 9.08 KB

Mockito v4 migration aids

Similarly to how IntelliJ provides inspections for Java language level migration aids, Mockitools also provides some inspections to help migrate from one version to another; in the context of this document, to 4.0.0.

Check out the Mockito 4.0.0 release notes for details.

Argument matchers are called via org.mockito.Matchers instead of org.mockito.ArgumentMatchers

Since org.mockito.Matchers is removed in 4.0.0, it is only org.mockito.ArgumentMatchers that is left to use.

This inspection, besides highlighting matchers called via Matchers, also provides a quick fix to replace the call to be via ArgumentMatchers.

//from:
Mockito.when(mock.method(org.mockito.Matchers.anyString())).thenReturn(10);
//to: since Matchers was not referenced via import, ArgumentMatchers is kept that way too
Mockito.when(mock.method(org.mockito.ArgumentMatchers.anyString())).thenReturn(10);
//from:
Mockito.when(mock.method(Matchers.anyString())).thenReturn(10);
//to: org.mockito.ArgumentMatchers is also imported, if necessary
Mockito.when(mock.method(ArgumentMatchers.anyString())).thenReturn(10);

Usage of deprecated anyX() matchers

anyObject/anyVararg -> any

anyObject() and anyVararg() are removed in 4.0.0, and it is advised to use any() instead.

The related quick fix can replace anyObject() and anyVararg() with any().

//from:
Mockito.when(mock.method(org.mockito.ArgumentMatchers.anyObject())).thenReturn(10);
Mockito.when(mock.method(ArgumentMatchers.anyVararg())).thenReturn(10);
//to:
Mockito.when(mock.method(org.mockito.ArgumentMatchers.any())).thenReturn(10);
Mockito.when(mock.method(ArgumentMatchers.any())).thenReturn(10);

anyXOf -> anyX

anyCollectionOf(), anyIterableOf(), anyListOf(), anyMapOf() and anySetOf() are removed in 4.0.0, and it is advised to use their anyX() variants instead.

The related quick fix can replace these matchers with their proper anyX() variants (anyCollection(), anyIterable(), etc.).

//from:
Mockito.when(mock.method(ArgumentMatchers.anyCollectionOf(String.class))).thenReturn(10);
//to:
Mockito.when(mock.method(ArgumentMatchers.anyCollection())).thenReturn(10);

Notes

If a matcher is not used with static import and referenced via org.mockito.Matchers, it is also replaced with the org.mockito.ArgumentMatchers qualifier. See the corresponding logic in ReplaceAnyXOfWithAnyXQuickFix.

Usage of parameterized variants of isNull(), isNotNull() and notNull()

isNull(Class), isNotNull(Class) and notNull(Class) are removed in 4.0.0, and it is advised to use their non-parameterized variants instead.

The related quick fix removes the method call argument, and updates the matcher to be called from org.mockito.ArgumentMatchers instead of org.mockito.Matchers, if necessary.

//from:
Mockito.when(mock.method(ArgumentMatchers.isNull(String.class))).thenReturn(10);
Mockito.when(mock.method(ArgumentMatchers.isNotNull(String.class))).thenReturn(10);
Mockito.when(mock.method(ArgumentMatchers.notNull(String.class))).thenReturn(10);
//to:
Mockito.when(mock.method(ArgumentMatchers.isNull())).thenReturn(10);
Mockito.when(mock.method(ArgumentMatchers.isNotNull())).thenReturn(10);
Mockito.when(mock.method(ArgumentMatchers.notNull())).thenReturn(10);

Usage of deprecated verify methods

MockedStatic.verify()

In Mockito v3 there are two verify() methods in MockedStatic with different signatures. MockedStatic.verify(VerificationMode, Verification) is removed in 4.0.0, thus it is advised to use verify with the other signature.

The related quick fix switches the two arguments of the verify call.

//from:
try (MockedStatic<Util> util = Mockito.mockStatic(Util.class)) {
    util.verify(Mockito.times(1), () -> Util.method(List.of()));
}
//to:
try (MockedStatic<Util> util = Mockito.mockStatic(Util.class)) {
    util.verify(() -> Util.method(List.of()), Mockito.times(1));
}

Mockito.verifyZeroInteractions()

verifyZeroInteractions() is deprecated since Mockito 3.0.1, and is removed in 4.0.0, thus it is advised to use verifyNoMoreInteractions() which it was an alias for.

The related quick fix replaces verifyZeroInteractions with verifyZeroInteractions keeping all its argument values.

//from:
Mockito.verifyZeroInteractions(mock1, mock2);
//to:
Mockito.verifyNoMoreInteractions(mock1, mock2);

Usage of deprecated plugins classes

org.mockito.configuration.AnnotationEngine and org.mockito.plugins.InstantiatorProvider are deprecated since Mockito v2, and are removed in 4.0.0.

AnnotationEngine

This quick fix replaces either the whole import statement, if possible, to minimize migration work, or if the reference is qualified, then replaces only that single reference.

//from:
import org.mockito.configuration.AnnotationEngine;

public interface CustomAnnotationEngine extends AnnotationEngine {
}

//to:
import org.mockito.plugins.AnnotationEngine;

public interface CustomAnnotationEngine extends AnnotationEngine {
}

InstantiatorProvider

Due to no name collision (unlike in case of AnnotationEngine), it is only a single reference that is replaced by this quick fix.

//from:
import org.mockito.plugins.InstantiatorProvider;

public interface CustomInstantiatorProvider extends InstantiatorProvider {
}

//to:
import org.mockito.plugins.InstantiatorProvider; //this import is left in for now
import org.mockito.plugins.InstantiatorProvider2;

public interface CustomInstantiatorProvider extends InstantiatorProvider2 {
}

Usage of deprecated JUnit runners

All JUnit runners in the org.mockito.runners package, and the package itself are deleted in Mockito 4.0.0, and it is advised to use org.mockito.junit.MockitJUnitRunner instead, to which the related quick fixes replace those runners.

//from:
import org.junit.runner.RunWith;
import org.mockito.runners.ConsoleSpammingMockitoJUnitRunner;

@RunWith(ConsoleSpammingMockitoJUnitRunner.class)
public class JUnitRunnerTest {
}

//to:
import org.junit.runner.RunWith;
import org.mockito.runners.ConsoleSpammingMockitoJUnitRunner; //this import is left in for now
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class JUnitRunnerTest {
}