Skip to content

Commit f7651e2

Browse files
authored
HADOOP-19243. Upgrade Mockito version to 4.11.0 (#6968)
Mockito is now at a JDK-17 compatible version. Contributed by Muskan Mishra
1 parent 51ebc3c commit f7651e2

File tree

84 files changed

+266
-143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+266
-143
lines changed

hadoop-client-modules/hadoop-client-minicluster/pom.xml

+11-2
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@
407407
<!-- Add back in Mockito since the hadoop-hdfs test jar needs it. -->
408408
<dependency>
409409
<groupId>org.mockito</groupId>
410-
<artifactId>mockito-core</artifactId>
410+
<artifactId>mockito-inline</artifactId>
411411
<optional>true</optional>
412412
</dependency>
413413
<!-- Add back in the transitive dependencies excluded from hadoop-common in client TODO remove once we have a filter for "is in these artifacts" -->
@@ -765,7 +765,7 @@
765765

766766
<!-- Mockito tries to include its own unrelocated copy of hamcrest. :( -->
767767
<filter>
768-
<artifact>org.mockito:mockito-core</artifact>
768+
<artifact>org.mockito:mockito-inline</artifact>
769769
<excludes>
770770
<exclude>asm-license.txt</exclude>
771771
<exclude>cglib-license.txt</exclude>
@@ -777,6 +777,15 @@
777777
<exclude>org/objenesis/*.class</exclude>
778778
</excludes>
779779
</filter>
780+
<!-- Additional filters to exclude unexpected contents -->
781+
<filter>
782+
<artifact>*:*</artifact>
783+
<excludes>
784+
<exclude>mockito-extensions/**</exclude>
785+
<exclude>win32-x86/**</exclude>
786+
<exclude>win32-x86-64/**</exclude>
787+
</excludes>
788+
</filter>
780789
<!-- skip grizzly internals we don't need to run. -->
781790
<filter>
782791
<artifact>org.glassfish.grizzly:grizzly-http-servlet</artifact>

hadoop-cloud-storage-project/hadoop-huaweicloud/pom.xml

+15-3
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,27 @@
173173
</dependency>
174174
<dependency>
175175
<groupId>org.powermock</groupId>
176-
<artifactId>powermock-api-mockito</artifactId>
177-
<version>1.7.4</version>
176+
<artifactId>powermock-api-mockito2</artifactId>
177+
<version>2.0.9</version>
178178
<scope>test</scope>
179+
<exclusions>
180+
<exclusion>
181+
<groupId>org.mockito</groupId>
182+
<artifactId>mockito-core</artifactId>
183+
</exclusion>
184+
</exclusions>
179185
</dependency>
180186
<dependency>
181187
<groupId>org.powermock</groupId>
182188
<artifactId>powermock-module-junit4</artifactId>
183-
<version>1.7.4</version>
189+
<version>2.0.9</version>
184190
<scope>test</scope>
191+
<exclusions>
192+
<exclusion>
193+
<groupId>org.mockito</groupId>
194+
<artifactId>mockito-core</artifactId>
195+
</exclusion>
196+
</exclusions>
185197
</dependency>
186198
</dependencies>
187199
</project>

hadoop-common-project/hadoop-auth/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<dependency>
5050
<groupId>org.mockito</groupId>
5151
<artifactId>mockito-core</artifactId>
52+
<version>4.11.0</version>
5253
<scope>test</scope>
5354
</dependency>
5455
<dependency>

hadoop-common-project/hadoop-common/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@
251251
</dependency>
252252
<dependency>
253253
<groupId>org.mockito</groupId>
254-
<artifactId>mockito-core</artifactId>
254+
<artifactId>mockito-inline</artifactId>
255255
<scope>test</scope>
256256
</dependency>
257257
<dependency>

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestServer.java

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.junit.Test;
3636
import org.slf4j.Logger;
3737

38+
import static org.apache.hadoop.test.MockitoUtil.verifyZeroInteractions;
39+
3840
/**
3941
* This is intended to be a set of unit tests for the
4042
* org.apache.hadoop.ipc.Server class.

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/http/TestCrossOriginFilter.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.junit.Test;
3737
import org.mockito.Mockito;
3838

39+
import static org.apache.hadoop.test.MockitoUtil.verifyZeroInteractions;
40+
3941
public class TestCrossOriginFilter {
4042

4143
@Test
@@ -59,7 +61,7 @@ public void testSameOrigin() throws ServletException, IOException {
5961
filter.init(filterConfig);
6062
filter.doFilter(mockReq, mockRes, mockChain);
6163

62-
Mockito.verifyZeroInteractions(mockRes);
64+
verifyZeroInteractions(mockRes);
6365
Mockito.verify(mockChain).doFilter(mockReq, mockRes);
6466
}
6567

@@ -224,7 +226,7 @@ public void testDisallowedOrigin() throws ServletException, IOException {
224226
filter.init(filterConfig);
225227
filter.doFilter(mockReq, mockRes, mockChain);
226228

227-
Mockito.verifyZeroInteractions(mockRes);
229+
verifyZeroInteractions(mockRes);
228230
Mockito.verify(mockChain).doFilter(mockReq, mockRes);
229231
}
230232

@@ -252,7 +254,7 @@ public void testDisallowedMethod() throws ServletException, IOException {
252254
filter.init(filterConfig);
253255
filter.doFilter(mockReq, mockRes, mockChain);
254256

255-
Mockito.verifyZeroInteractions(mockRes);
257+
verifyZeroInteractions(mockRes);
256258
Mockito.verify(mockChain).doFilter(mockReq, mockRes);
257259
}
258260

@@ -283,7 +285,7 @@ public void testDisallowedHeader() throws ServletException, IOException {
283285
filter.init(filterConfig);
284286
filter.doFilter(mockReq, mockRes, mockChain);
285287

286-
Mockito.verifyZeroInteractions(mockRes);
288+
verifyZeroInteractions(mockRes);
287289
Mockito.verify(mockChain).doFilter(mockReq, mockRes);
288290
}
289291

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/http/TestRestCsrfPreventionFilter.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.junit.Test;
3333
import org.mockito.Mockito;
3434

35+
import static org.apache.hadoop.test.MockitoUtil.verifyZeroInteractions;
36+
3537
/**
3638
* This class tests the behavior of the RestCsrfPreventionFilter.
3739
*
@@ -75,7 +77,7 @@ public void testNoHeaderDefaultConfigBadRequest()
7577

7678
verify(mockRes, atLeastOnce()).sendError(
7779
HttpServletResponse.SC_BAD_REQUEST, EXPECTED_MESSAGE);
78-
Mockito.verifyZeroInteractions(mockChain);
80+
verifyZeroInteractions(mockChain);
7981
}
8082

8183
@Test
@@ -110,7 +112,7 @@ public void testNoHeaderCustomAgentConfigBadRequest()
110112

111113
verify(mockRes, atLeastOnce()).sendError(
112114
HttpServletResponse.SC_BAD_REQUEST, EXPECTED_MESSAGE);
113-
Mockito.verifyZeroInteractions(mockChain);
115+
verifyZeroInteractions(mockChain);
114116
}
115117

116118
@Test
@@ -228,7 +230,7 @@ public void testMissingHeaderWithCustomHeaderConfigBadRequest()
228230
filter.init(filterConfig);
229231
filter.doFilter(mockReq, mockRes, mockChain);
230232

231-
Mockito.verifyZeroInteractions(mockChain);
233+
verifyZeroInteractions(mockChain);
232234
}
233235

234236
@Test
@@ -260,7 +262,7 @@ public void testMissingHeaderNoMethodsToIgnoreConfigBadRequest()
260262
filter.init(filterConfig);
261263
filter.doFilter(mockReq, mockRes, mockChain);
262264

263-
Mockito.verifyZeroInteractions(mockChain);
265+
verifyZeroInteractions(mockChain);
264266
}
265267

266268
@Test
@@ -356,6 +358,6 @@ public void testMissingHeaderMultipleIgnoreMethodsConfigBadRequest()
356358
filter.init(filterConfig);
357359
filter.doFilter(mockReq, mockRes, mockChain);
358360

359-
Mockito.verifyZeroInteractions(mockChain);
361+
verifyZeroInteractions(mockChain);
360362
}
361363
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MockitoUtil.java

+9
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,13 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
6161
}
6262
});
6363
}
64+
65+
/**
66+
* Verifies that there were no interactions with the given mock objects.
67+
*
68+
* @param mocks the mock objects to verify
69+
*/
70+
public static void verifyZeroInteractions(Object... mocks) {
71+
Mockito.verifyNoInteractions(mocks);
72+
}
6473
}

hadoop-common-project/hadoop-kms/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
</dependency>
4646
<dependency>
4747
<groupId>org.mockito</groupId>
48-
<artifactId>mockito-core</artifactId>
48+
<artifactId>mockito-inline</artifactId>
4949
<scope>test</scope>
5050
</dependency>
5151
<dependency>

hadoop-common-project/hadoop-nfs/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
</dependency>
6161
<dependency>
6262
<groupId>org.mockito</groupId>
63-
<artifactId>mockito-core</artifactId>
63+
<artifactId>mockito-inline</artifactId>
6464
<scope>test</scope>
6565
</dependency>
6666
<dependency>

hadoop-hdfs-project/hadoop-hdfs-client/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
6666
</dependency>
6767
<dependency>
6868
<groupId>org.mockito</groupId>
69-
<artifactId>mockito-core</artifactId>
69+
<artifactId>mockito-inline</artifactId>
7070
<scope>test</scope>
7171
</dependency>
7272
<dependency>

hadoop-hdfs-project/hadoop-hdfs-httpfs/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
</dependency>
5050
<dependency>
5151
<groupId>org.mockito</groupId>
52-
<artifactId>mockito-core</artifactId>
52+
<artifactId>mockito-inline</artifactId>
5353
<scope>test</scope>
5454
</dependency>
5555
<dependency>

hadoop-hdfs-project/hadoop-hdfs-native-client/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
6969
</dependency>
7070
<dependency>
7171
<groupId>org.mockito</groupId>
72-
<artifactId>mockito-core</artifactId>
72+
<artifactId>mockito-inline</artifactId>
7373
<scope>test</scope>
7474
</dependency>
7575
<dependency>

hadoop-hdfs-project/hadoop-hdfs-nfs/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
160160
</dependency>
161161
<dependency>
162162
<groupId>org.mockito</groupId>
163-
<artifactId>mockito-core</artifactId>
163+
<artifactId>mockito-inline</artifactId>
164164
<scope>test</scope>
165165
</dependency>
166166
<dependency>

hadoop-hdfs-project/hadoop-hdfs-rbf/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
164164
</dependency>
165165
<dependency>
166166
<groupId>org.mockito</groupId>
167-
<artifactId>mockito-core</artifactId>
167+
<artifactId>mockito-inline</artifactId>
168168
<scope>test</scope>
169169
</dependency>
170170
<dependency>

hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterAdmin.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.junit.Assert.assertTrue;
2626

2727
import java.io.IOException;
28+
import java.lang.reflect.Field;
2829
import java.security.PrivilegedExceptionAction;
2930
import java.util.Collections;
3031
import java.util.HashMap;
@@ -68,7 +69,6 @@
6869
import org.junit.BeforeClass;
6970
import org.junit.Test;
7071
import org.mockito.Mockito;
71-
import org.mockito.internal.util.reflection.FieldSetter;
7272

7373
/**
7474
* The administrator interface of the {@link Router} implemented by
@@ -118,18 +118,25 @@ public static void globalSetUp() throws Exception {
118118
* @throws IOException
119119
* @throws NoSuchFieldException
120120
*/
121-
private static void setUpMocks() throws IOException, NoSuchFieldException {
121+
public static void setField(Object target, String fieldName, Object value)
122+
throws NoSuchFieldException, IllegalAccessException {
123+
Field field = target.getClass().getDeclaredField(fieldName);
124+
field.setAccessible(true);
125+
field.set(target, value);
126+
}
127+
128+
private static void setUpMocks()
129+
throws IOException, NoSuchFieldException, IllegalAccessException {
122130
RouterRpcServer spyRpcServer =
123131
Mockito.spy(routerContext.getRouter().createRpcServer());
124-
FieldSetter.setField(routerContext.getRouter(),
125-
Router.class.getDeclaredField("rpcServer"), spyRpcServer);
132+
//Used reflection to set the 'rpcServer field'
133+
setField(routerContext.getRouter(), "rpcServer", spyRpcServer);
126134
Mockito.doReturn(null).when(spyRpcServer).getFileInfo(Mockito.anyString());
127135

128136
// mock rpc client for destination check when editing mount tables.
137+
//spy RPC client and used reflection to set the 'rpcClient' field
129138
mockRpcClient = Mockito.spy(spyRpcServer.getRPCClient());
130-
FieldSetter.setField(spyRpcServer,
131-
RouterRpcServer.class.getDeclaredField("rpcClient"),
132-
mockRpcClient);
139+
setField(spyRpcServer, "rpcClient", mockRpcClient);
133140
RemoteLocation remoteLocation0 =
134141
new RemoteLocation("ns0", "/testdir", null);
135142
RemoteLocation remoteLocation1 =

hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterRpcMultiDestination.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import static org.junit.Assert.assertNotNull;
2525
import static org.junit.Assert.assertTrue;
2626
import static org.junit.Assert.fail;
27-
import static org.mockito.Matchers.any;
27+
import static org.mockito.ArgumentMatchers.any;
2828
import static org.mockito.Mockito.doThrow;
2929
import static org.mockito.Mockito.mock;
3030
import static org.apache.hadoop.test.Whitebox.getInternalState;

hadoop-hdfs-project/hadoop-hdfs/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
166166
</dependency>
167167
<dependency>
168168
<groupId>org.mockito</groupId>
169-
<artifactId>mockito-core</artifactId>
169+
<artifactId>mockito-inline</artifactId>
170170
<scope>test</scope>
171171
</dependency>
172172
<dependency>

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/INodeFile.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,8 @@ static long toLong(long preferredBlockSize, long layoutRedundancy,
253253

254254
private BlockInfo[] blocks;
255255

256-
INodeFile(long id, byte[] name, PermissionStatus permissions, long mtime,
257-
long atime, BlockInfo[] blklist, short replication,
258-
long preferredBlockSize) {
256+
public INodeFile(long id, byte[] name, PermissionStatus permissions, long mtime, long atime,
257+
BlockInfo[] blklist, short replication, long preferredBlockSize) {
259258
this(id, name, permissions, mtime, atime, blklist, replication, null,
260259
preferredBlockSize, (byte) 0, CONTIGUOUS);
261260
}

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockManagerSafeMode.java

+1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ public void testIncrementAndDecrementStripedSafeBlockCount() {
365365
for (long i = 1; i <= BLOCK_TOTAL; i++) {
366366
BlockInfoStriped blockInfo = mock(BlockInfoStriped.class);
367367
when(blockInfo.getRealDataBlockNum()).thenReturn(realDataBlockNum);
368+
when(blockInfo.isStriped()).thenReturn(true);
368369

369370
bmSafeMode.incrementSafeBlockCount(realDataBlockNum, blockInfo);
370371
bmSafeMode.decrementSafeBlockCount(blockInfo);

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestCacheDirectives.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_CACHING_ENABLED_KEY;
2626
import static org.apache.hadoop.hdfs.protocol.CachePoolInfo.RELATIVE_EXPIRY_NEVER;
2727
import static org.apache.hadoop.test.GenericTestUtils.assertExceptionContains;
28+
import static org.apache.hadoop.test.MockitoUtil.verifyZeroInteractions;
2829
import static org.junit.Assert.assertEquals;
2930
import static org.junit.Assert.assertFalse;
3031
import static org.junit.Assert.assertNotNull;
@@ -1575,7 +1576,7 @@ public void testNoLookupsWhenNotUsed() throws Exception {
15751576
CacheManager cm = cluster.getNamesystem().getCacheManager();
15761577
LocatedBlocks locations = Mockito.mock(LocatedBlocks.class);
15771578
cm.setCachedLocations(locations);
1578-
Mockito.verifyZeroInteractions(locations);
1579+
verifyZeroInteractions(locations);
15791580
}
15801581

15811582
@Test(timeout=120000)

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestCommitBlockSynchronization.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ private FSNamesystem makeNameSystemSpy(Block block, INodeFile file)
6262
}
6363
namesystem.dir.getINodeMap().put(file);
6464

65-
FSNamesystem namesystemSpy = spy(namesystem);
6665
BlockInfo blockInfo = new BlockInfoContiguous(block, (short) 1);
6766
blockInfo.convertToBlockUnderConstruction(
6867
HdfsServerConstants.BlockUCState.UNDER_CONSTRUCTION, targets);
@@ -73,8 +72,10 @@ private FSNamesystem makeNameSystemSpy(Block block, INodeFile file)
7372
doReturn(blockInfo).when(file).removeLastBlock(any(Block.class));
7473
doReturn(true).when(file).isUnderConstruction();
7574
doReturn(new BlockInfoContiguous[1]).when(file).getBlocks();
76-
77-
doReturn(blockInfo).when(namesystemSpy).getStoredBlock(any(Block.class));
75+
FSNamesystem namesystemSpy = spy(namesystem);
76+
doReturn(blockInfo).when(namesystemSpy).getStoredBlock(nullable(Block.class));
77+
doReturn(file).when(namesystemSpy).getBlockCollection(any(BlockInfo.class));
78+
doReturn(false).when(namesystemSpy).isFileDeleted(any(INodeFile.class));
7879
doReturn(blockInfo).when(file).getLastBlock();
7980
doNothing().when(namesystemSpy).closeFileCommitBlocks(
8081
any(), any(INodeFile.class), any(BlockInfo.class));

0 commit comments

Comments
 (0)