Skip to content

Migration from Mockito 1 to Mockito 4

Marcello Rinaldo Martina edited this page Nov 25, 2022 · 1 revision

Mockito has been updated in Kura 5.3 from 1 to version 4. The migration of your existing tests can be made going through the following steps. This is not an exhaustive list, please contribute to the document for new discoveries.

Update the pom.xml of your tests by updating the dependencies:

<dependency>
	<groupId>org.mockito</groupId>
	<artifactId>mockito-core</artifactId>
	<version>4.8.1</version>
	<scope>test</scope>
</dependency>

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.13.2</version>
	<scope>test</scope>
</dependency>

Matchers API becomes ArgumentMatchers:

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyObject;
// become
import static org.mockito.ArgumentMatchers.any;

...

import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyLong;
// become
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;

...

import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.isA;
import static org.mockito.Matchers.notNull;
// become
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.ArgumentMatchers.notNull;

InvocationOnMock API change:

InvocationOnMock.getArgumentAt(int index, Class<Tclazz)
// becomes
InvocationOnMock.getArgument(int index, Class<Tclazz)

ArgumentMatcher is now an interface:

public class Example extends org.mockito.ArgumentMatcher<ExampleType> {

}
// becomes
public class Example implements org.mockito.ArgumentMatcher<ExampleType> {

}
Clone this wiki locally