or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

tessl/maven-org-mockito--mockito-core

Mockito mock objects library core API and implementation for comprehensive Java unit testing

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.mockito/mockito-core@5.19.x

To install, run

npx @tessl/cli install tessl/maven-org-mockito--mockito-core@5.19.0

0

# Mockito Core

1

2

Mockito Core is the most popular mocking framework for Java. It enables developers to create mock objects, verify interactions, and stub method behaviors for comprehensive unit testing. The library offers a fluent API for creating mocks, spies, and stubs with features including argument matchers, verification methods, stubbing capabilities, and behavioral verification.

3

4

## Package Information

5

6

- **Package Name**: mockito-core

7

- **Package Type**: maven

8

- **Language**: Java

9

- **Installation**:

10

```xml

11

<dependency>

12

<groupId>org.mockito</groupId>

13

<artifactId>mockito-core</artifactId>

14

<version>5.19.0</version>

15

</dependency>

16

```

17

18

Gradle:

19

```gradle

20

testImplementation 'org.mockito:mockito-core:5.19.0'

21

```

22

23

## Core Imports

24

25

```java

26

import static org.mockito.Mockito.*;

27

import static org.mockito.ArgumentMatchers.*;

28

import org.mockito.Mock;

29

import org.mockito.InjectMocks;

30

import org.mockito.MockitoAnnotations;

31

```

32

33

For BDD-style testing:

34

```java

35

import static org.mockito.BDDMockito.*;

36

```

37

38

## Basic Usage

39

40

```java

41

import static org.mockito.Mockito.*;

42

import static org.mockito.ArgumentMatchers.*;

43

import org.junit.jupiter.api.Test;

44

45

class BasicMockitoExample {

46

47

@Test

48

void basicMocking() {

49

// Create mock

50

List<String> mockList = mock(List.class);

51

52

// Stub method behavior

53

when(mockList.get(0)).thenReturn("first");

54

when(mockList.size()).thenReturn(1);

55

56

// Use mock

57

System.out.println(mockList.get(0)); // prints "first"

58

System.out.println(mockList.size()); // prints 1

59

60

// Verify interactions

61

verify(mockList).get(0);

62

verify(mockList).size();

63

}

64

65

@Test

66

void verificationExample() {

67

List<String> mockList = mock(List.class);

68

69

// Use mock

70

mockList.add("one");

71

mockList.add("two");

72

mockList.clear();

73

74

// Verify specific interactions

75

verify(mockList).add("one");

76

verify(mockList).add("two");

77

verify(mockList).clear();

78

verify(mockList, times(2)).add(anyString());

79

}

80

}

81

```

82

83

## Architecture

84

85

Mockito Core consists of several key components:

86

87

- **Mock Creation**: The `Mockito` class provides static methods for creating mocks and spies

88

- **Stubbing**: `OngoingStubbing` and `Stubber` interfaces provide fluent APIs for defining mock behavior

89

- **Verification**: Built-in verification modes support different assertion patterns

90

- **Argument Matching**: Flexible matchers in `ArgumentMatchers` enable sophisticated stubbing and verification

91

- **Annotations**: `@Mock`, `@Spy`, `@InjectMocks` annotations simplify test setup

92

- **Advanced Features**: Static mocking, construction mocking, and custom answers for complex scenarios

93

94

## Capabilities

95

96

### Mock Creation and Basic Operations

97

98

Core functionality for creating and managing mock objects.

99

100

```java { .api }

101

public static <T> T mock(Class<T> classToMock)

102

public static <T> T mock(Class<T> classToMock, String name)

103

public static <T> T mock(Class<T> classToMock, Answer defaultAnswer)

104

public static <T> T spy(T object)

105

public static <T> T spy(Class<T> classToSpy)

106

public static MockSettings withSettings()

107

```

108

109

[Mock Creation and Configuration](./mock-creation.md)

110

111

### Method Stubbing

112

113

Configure mock object behavior with flexible stubbing options.

114

115

```java { .api }

116

public static <T> OngoingStubbing<T> when(T methodCall)

117

public static Stubber doReturn(Object toBeReturned)

118

public static Stubber doThrow(Throwable... toBeThrown)

119

public static Stubber doNothing()

120

public static Stubber doAnswer(Answer answer)

121

public static Stubber doCallRealMethod()

122

```

123

124

[Method Stubbing and Behavior](./stubbing.md)

125

126

### Verification and Assertions

127

128

Verify method invocations with flexible verification modes.

129

130

```java { .api }

131

public static <T> T verify(T mock)

132

public static <T> T verify(T mock, VerificationMode mode)

133

public static InOrder inOrder(Object... mocks)

134

public static void verifyNoMoreInteractions(Object... mocks)

135

public static void verifyNoInteractions(Object... mocks)

136

```

137

138

[Verification and Assertions](./verification.md)

139

140

### Argument Matching

141

142

Flexible argument matchers for stubbing and verification.

143

144

```java { .api }

145

public static <T> T any()

146

public static <T> T any(Class<T> type)

147

public static String anyString()

148

public static int anyInt()

149

public static boolean anyBoolean()

150

public static <T> T eq(T value)

151

public static <T> T argThat(ArgumentMatcher<T> matcher)

152

```

153

154

[Argument Matching](./argument-matching.md)

155

156

### Annotations and Test Setup

157

158

Annotation-based mock creation and dependency injection.

159

160

```java { .api }

161

@Mock

162

@Spy

163

@InjectMocks

164

@Captor

165

public static AutoCloseable openMocks(Object testClass)

166

```

167

168

[Annotations and Test Setup](./annotations.md)

169

170

### Static and Construction Mocking

171

172

Mock static methods and object construction (since Mockito 3.4.0+).

173

174

```java { .api }

175

public static <T> MockedStatic<T> mockStatic(Class<T> classToMock)

176

public static <T> MockedConstruction<T> mockConstruction(Class<T> classToMock)

177

```

178

179

[Static and Construction Mocking](./static-mocking.md)

180

181

### BDD-Style Testing

182

183

Behavior-driven development style API with given/when/then syntax.

184

185

```java { .api }

186

public static <T> BDDMyOngoingStubbing<T> given(T methodCall)

187

public static <T> Then<T> then(T mock)

188

public static Stubber willReturn(Object value)

189

public static Stubber willThrow(Throwable... throwables)

190

```

191

192

[BDD-Style Testing](./bdd-testing.md)

193

194

### Additional Argument Matchers

195

196

Advanced argument matchers for complex matching scenarios including numerical comparisons, array equality, and logical combinations.

197

198

```java { .api }

199

public static <T extends Comparable<T>> T geq(T value)

200

public static <T extends Comparable<T>> T leq(T value)

201

public static <T extends Comparable<T>> T gt(T value)

202

public static <T extends Comparable<T>> T lt(T value)

203

public static String find(String regex)

204

public static <T> T[] aryEq(T[] value)

205

public static <T> T and(T first, T second)

206

public static <T> T or(T first, T second)

207

public static <T> T not(T value)

208

```

209

210

[Additional Argument Matchers](./additional-matchers.md)

211

212

### Additional Answer Behaviors

213

214

Advanced stubbing behaviors including argument-based returns, call delegation, and functional interface answers.

215

216

```java { .api }

217

public static <T> Answer<T> returnsFirstArg()

218

public static <T> Answer<T> returnsSecondArg()

219

public static <T> Answer<T> returnsLastArg()

220

public static <T> Answer<T> returnsArgAt(int position)

221

public static <T> Answer<T> delegatesTo(Object delegate)

222

public static <T> Answer<T> returnsElementsOf(Collection<?> elements)

223

public static <T> Answer<T> answersWithDelay(long sleepyTime, Answer<T> answer)

224

```

225

226

[Additional Answer Behaviors](./additional-answers.md)

227

228

### Advanced Features

229

230

Custom answers, session management, and framework integration.

231

232

```java { .api }

233

public static MockitoSession mockitoSession()

234

public static MockitoFramework framework()

235

public static void validateMockitoUsage()

236

public static void reset(Object... mocks)

237

```

238

239

[Advanced Features and Integration](./advanced-features.md)

240

241

## Core Types

242

243

```java { .api }

244

interface MockSettings {

245

MockSettings name(String name);

246

MockSettings defaultAnswer(Answer defaultAnswer);

247

MockSettings extraInterfaces(Class<?>... interfaces);

248

MockSettings serializable();

249

MockSettings strictness(Strictness strictness);

250

MockSettings lenient();

251

}

252

253

interface OngoingStubbing<T> {

254

OngoingStubbing<T> thenReturn(T value);

255

OngoingStubbing<T> thenReturn(T value, T... values);

256

OngoingStubbing<T> thenThrow(Throwable... throwables);

257

OngoingStubbing<T> thenAnswer(Answer<?> answer);

258

OngoingStubbing<T> thenCallRealMethod();

259

}

260

261

interface VerificationMode {

262

void verify(VerificationData data);

263

VerificationMode description(String description);

264

}

265

266

interface Answer<T> {

267

T answer(InvocationOnMock invocation) throws Throwable;

268

}

269

270

interface InOrder {

271

<T> T verify(T mock);

272

<T> T verify(T mock, VerificationMode mode);

273

void verifyNoMoreInteractions();

274

}

275

```