Skip to content

Commit 55c8115

Browse files
authored
Update Mockito to 5.16.1 (#10686)
Dependency name change mockito-inline to mockito-core. Inline is now the default and the last version of mockito-inline released is 5.2.0. assertj-core in user-authenticators/saml2 pulls in an incompatible version of byte-buddy and required an exclusion. Updating the version of assertj is left for a future PR. The upgrade requires Java 11+, dropping support for Java 8. CloudStack documentation already says to use Java 11 and does not indicate that java 8 is supported. Test classes using @RunWith(MockitoJUnitRunner.class) now get run in strict mode. Changes were made to tests where the stubbing intention was clear. In ManagementServerMaintenanceManagerImplTest there are 5 tests where the intention of the test is unclear. Each of the statements now use Mockito.lenient() to avoid the exception. Other cases in the tests follow a similar pattern. Minor clean up. Both @SPY and Mockito.spy( should not be used. Favored the annotation. Both @RunWith(MockitoJUnitRunner.class) and MockitoAnnotations.openMocks(this); should not be used. Favored the annotation. Unnecessary extends TestCase removed. @Injectmocks and new in statement unnecessary. Removed new when issue presented. Some of the Cmd classes like UpdateNetworkCmd have a type tree that includes fields of type Object. This appears to cause issues with injection, requiring that @mock fields be available. This is where the following fields were added in multiple places: Object job; ResponseGenerator _responseGenerator; Wrong number of parameters for Mockito.when in LibvirtRevertSnapshotCommandWrapperTest.java
1 parent 41e881e commit 55c8115

File tree

24 files changed

+90
-87
lines changed

24 files changed

+90
-87
lines changed

api/src/test/java/org/apache/cloudstack/api/command/admin/resource/PurgeExpungedResourcesCmdTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class PurgeExpungedResourcesCmdTest {
4444

4545
@Spy
4646
@InjectMocks
47-
PurgeExpungedResourcesCmd spyCmd = Mockito.spy(new PurgeExpungedResourcesCmd());
47+
PurgeExpungedResourcesCmd spyCmd;
4848

4949
@Test
5050
public void testGetResourceType() {

api/src/test/java/org/apache/cloudstack/api/command/admin/storage/DownloadImageStoreObjectCmdTest.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@
2020
import com.cloud.utils.Pair;
2121
import org.apache.cloudstack.api.response.ExtractResponse;
2222
import org.apache.cloudstack.storage.browser.StorageBrowser;
23-
import org.junit.After;
2423
import org.junit.Assert;
25-
import org.junit.Before;
2624
import org.junit.Test;
2725
import org.junit.runner.RunWith;
2826
import org.mockito.InjectMocks;
2927
import org.mockito.Mock;
30-
import org.mockito.MockitoAnnotations;
3128
import org.mockito.Spy;
3229
import org.mockito.junit.MockitoJUnitRunner;
3330
import org.springframework.test.util.ReflectionTestUtils;
@@ -48,18 +45,6 @@ public class DownloadImageStoreObjectCmdTest {
4845
@Spy
4946
private DownloadImageStoreObjectCmd cmd;
5047

51-
private AutoCloseable closeable;
52-
53-
@Before
54-
public void setUp() {
55-
closeable = MockitoAnnotations.openMocks(this);
56-
}
57-
58-
@After
59-
public void tearDown() throws Exception {
60-
closeable.close();
61-
}
62-
6348
@Test
6449
public void testExecute() throws Exception {
6550
ReflectionTestUtils.setField(cmd, "storeId", 1L);

api/src/test/java/org/apache/cloudstack/api/command/admin/vm/MigrateVirtualMachineWithVolumeCmdTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public class MigrateVirtualMachineWithVolumeCmdTest {
6868
@Mock
6969
Host hostMock;
7070

71+
@Mock
72+
private Object job;
73+
74+
@Mock
75+
private Object _responseObject;
76+
7177
@Spy
7278
@InjectMocks
7379
MigrateVirtualMachineWithVolumeCmd cmdSpy;

api/src/test/java/org/apache/cloudstack/api/command/admin/vpc/CreateVPCCmdByAdminTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.cloud.network.vpc.VpcService;
2121
import com.cloud.user.AccountService;
2222
import com.cloud.utils.db.EntityManager;
23-
import junit.framework.TestCase;
2423
import org.apache.cloudstack.api.ResponseGenerator;
2524
import org.junit.Assert;
2625
import org.junit.Test;
@@ -34,7 +33,7 @@
3433
import java.util.List;
3534

3635
@RunWith(MockitoJUnitRunner.class)
37-
public class CreateVPCCmdByAdminTest extends TestCase {
36+
public class CreateVPCCmdByAdminTest {
3837

3938
@Mock
4039
public VpcService _vpcService;
@@ -43,8 +42,10 @@ public class CreateVPCCmdByAdminTest extends TestCase {
4342
@Mock
4443
public AccountService _accountService;
4544
private ResponseGenerator responseGenerator;
45+
@Mock
46+
public Object job;
4647
@InjectMocks
47-
CreateVPCCmdByAdmin cmd = new CreateVPCCmdByAdmin();
48+
CreateVPCCmdByAdmin cmd;
4849

4950
@Test
5051
public void testBgpPeerIds() {

api/src/test/java/org/apache/cloudstack/api/command/user/network/UpdateNetworkCmdTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ public class UpdateNetworkCmdTest {
4141
NetworkService networkService;
4242
@Mock
4343
public EntityManager _entityMgr;
44-
private ResponseGenerator responseGenerator;
44+
@Mock
45+
private ResponseGenerator _responseGenerator;
46+
@Mock
47+
private Object job;
4548
@InjectMocks
4649
UpdateNetworkCmd cmd = new UpdateNetworkCmd();
4750

@@ -176,15 +179,13 @@ public void testExecute() throws InsufficientCapacityException {
176179
ReflectionTestUtils.setField(cmd, "id", networkId);
177180
ReflectionTestUtils.setField(cmd, "publicMtu", publicmtu);
178181
Network network = Mockito.mock(Network.class);
179-
responseGenerator = Mockito.mock(ResponseGenerator.class);
180182
NetworkResponse response = Mockito.mock(NetworkResponse.class);
181183
response.setPublicMtu(publicmtu);
182184
Mockito.when(networkService.getNetwork(networkId)).thenReturn(network);
183185
Mockito.when(networkService.updateGuestNetwork(cmd)).thenReturn(network);
184-
cmd._responseGenerator = responseGenerator;
185-
Mockito.when(responseGenerator.createNetworkResponse(ResponseObject.ResponseView.Restricted, network)).thenReturn(response);
186+
Mockito.when(_responseGenerator.createNetworkResponse(ResponseObject.ResponseView.Restricted, network)).thenReturn(response);
186187
cmd.execute();
187-
Mockito.verify(responseGenerator).createNetworkResponse(Mockito.any(ResponseObject.ResponseView.class), Mockito.any(Network.class));
188+
Mockito.verify(_responseGenerator).createNetworkResponse(Mockito.any(ResponseObject.ResponseView.class), Mockito.any(Network.class));
188189
NetworkResponse actualResponse = (NetworkResponse) cmd.getResponseObject();
189190

190191
Assert.assertEquals(response, actualResponse);

api/src/test/java/org/apache/cloudstack/api/command/user/vpc/CreateVPCCmdTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.cloud.network.vpc.VpcService;
2626
import com.cloud.user.AccountService;
2727
import com.cloud.utils.db.EntityManager;
28-
import junit.framework.TestCase;
2928
import org.apache.cloudstack.api.ResponseGenerator;
3029
import org.apache.cloudstack.api.ResponseObject;
3130
import org.apache.cloudstack.api.response.VpcResponse;
@@ -39,17 +38,21 @@
3938
import org.springframework.test.util.ReflectionTestUtils;
4039

4140
@RunWith(MockitoJUnitRunner.class)
42-
public class CreateVPCCmdTest extends TestCase {
41+
public class CreateVPCCmdTest {
4342

4443
@Mock
4544
public VpcService _vpcService;
4645
@Mock
4746
public EntityManager _entityMgr;
4847
@Mock
4948
public AccountService _accountService;
50-
private ResponseGenerator responseGenerator;
49+
@Mock
50+
private ResponseGenerator _responseGenerator;
51+
@Mock
52+
private Object job;
53+
5154
@InjectMocks
52-
CreateVPCCmd cmd = new CreateVPCCmd();
55+
CreateVPCCmd cmd;
5356

5457
@Test
5558
public void testGetAccountName() {
@@ -185,11 +188,9 @@ public void testExecute() throws ResourceUnavailableException, InsufficientCapac
185188
VpcResponse response = Mockito.mock(VpcResponse.class);
186189

187190
ReflectionTestUtils.setField(cmd, "id", 1L);
188-
responseGenerator = Mockito.mock(ResponseGenerator.class);
189191
Mockito.doNothing().when(_vpcService).startVpc(cmd);
190192
Mockito.when(_entityMgr.findById(Mockito.eq(Vpc.class), Mockito.any(Long.class))).thenReturn(vpc);
191-
cmd._responseGenerator = responseGenerator;
192-
Mockito.when(responseGenerator.createVpcResponse(ResponseObject.ResponseView.Restricted, vpc)).thenReturn(response);
193+
Mockito.when(_responseGenerator.createVpcResponse(ResponseObject.ResponseView.Restricted, vpc)).thenReturn(response);
193194
cmd.execute();
194195
Mockito.verify(_vpcService, Mockito.times(1)).startVpc(cmd);
195196
}

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ private void configureAndVerifyTestSearchDiskDefOnMigrateDiskInfoList(String ser
704704

705705
@Test
706706
public void deleteOrDisconnectDisksOnSourcePoolTest() {
707-
LibvirtMigrateCommandWrapper spyLibvirtMigrateCmdWrapper = Mockito.spy(libvirtMigrateCmdWrapper);
707+
LibvirtMigrateCommandWrapper spyLibvirtMigrateCmdWrapper = libvirtMigrateCmdWrapper;
708708
Mockito.doNothing().when(spyLibvirtMigrateCmdWrapper).deleteLocalVolume("volPath");
709709

710710
List<MigrateDiskInfo> migrateDiskInfoList = new ArrayList<>();

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRevertSnapshotCommandWrapperTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.mockito.Mock;
3333
import org.mockito.MockedStatic;
3434
import org.mockito.Mockito;
35+
import org.mockito.Spy;
3536
import org.mockito.junit.MockitoJUnitRunner;
3637

3738
import com.cloud.agent.api.to.DataStoreTO;
@@ -42,7 +43,8 @@
4243
@RunWith(MockitoJUnitRunner.class)
4344
public class LibvirtRevertSnapshotCommandWrapperTest {
4445

45-
LibvirtRevertSnapshotCommandWrapper libvirtRevertSnapshotCommandWrapperSpy = Mockito.spy(LibvirtRevertSnapshotCommandWrapper.class);
46+
@Spy
47+
LibvirtRevertSnapshotCommandWrapper libvirtRevertSnapshotCommandWrapperSpy;
4648

4749
@Mock
4850
KVMStoragePool kvmStoragePoolPrimaryMock;
@@ -107,7 +109,7 @@ public void validateGetSnapshotPathExistsOnPrimaryStorage() {
107109
Mockito.doReturn(snapshotPath).when(snapshotObjectToPrimaryMock).getPath();
108110

109111
try (MockedStatic<Files> ignored = Mockito.mockStatic(Files.class)) {
110-
Mockito.when(Files.exists(Mockito.any(Path.class), Mockito.any())).thenReturn(true);
112+
Mockito.when(Files.exists(Mockito.any(Path.class))).thenReturn(true);
111113

112114
Pair<String, SnapshotObjectTO> result = libvirtRevertSnapshotCommandWrapperSpy.getSnapshot(
113115
snapshotObjectToPrimaryMock, snapshotObjectToSecondaryMock,

plugins/hypervisors/vmware/src/test/java/com/cloud/hypervisor/vmware/manager/VmwareManagerImplTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public class VmwareManagerImplTest {
6969
private Map<String, String> clusterDetails;
7070
@Mock
7171
private Map<String, String> hostDetails;
72+
@Mock
73+
private Map<String, Object> _configParams;
7274

7375
@Before
7476
public void beforeTest() {

plugins/hypervisors/vmware/src/test/java/com/cloud/hypervisor/vmware/resource/VmwareResourceTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import java.util.List;
4242
import java.util.Map;
4343

44+
import com.cloud.agent.api.Command;
45+
import com.cloud.agent.api.ScaleVmAnswer;
4446
import com.cloud.hypervisor.vmware.mo.DatastoreMO;
4547
import com.cloud.hypervisor.vmware.mo.HostDatastoreBrowserMO;
4648
import com.cloud.hypervisor.vmware.mo.HypervisorHostHelper;
@@ -52,7 +54,6 @@
5254
import org.apache.cloudstack.storage.command.browser.ListDataStoreObjectsAnswer;
5355
import org.apache.cloudstack.storage.command.browser.ListDataStoreObjectsCommand;
5456
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
55-
import org.junit.After;
5657
import org.junit.Before;
5758
import org.junit.Test;
5859
import org.junit.runner.RunWith;
@@ -62,17 +63,14 @@
6263
import org.mockito.MockedConstruction;
6364
import org.mockito.MockedStatic;
6465
import org.mockito.Mockito;
65-
import org.mockito.MockitoAnnotations;
6666
import org.mockito.Spy;
6767
import org.mockito.junit.MockitoJUnitRunner;
6868

6969
import com.cloud.agent.api.Answer;
70-
import com.cloud.agent.api.Command;
7170
import com.cloud.agent.api.CheckGuestOsMappingAnswer;
7271
import com.cloud.agent.api.CheckGuestOsMappingCommand;
7372
import com.cloud.agent.api.GetHypervisorGuestOsNamesAnswer;
7473
import com.cloud.agent.api.GetHypervisorGuestOsNamesCommand;
75-
import com.cloud.agent.api.ScaleVmAnswer;
7674
import com.cloud.agent.api.ScaleVmCommand;
7775
import com.cloud.agent.api.routing.GetAutoScaleMetricsAnswer;
7876
import com.cloud.agent.api.routing.GetAutoScaleMetricsCommand;
@@ -185,6 +183,8 @@ public VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd) {
185183
VimPortType vimService;
186184
@Mock
187185
HostCapability hostCapability;
186+
@Mock
187+
ManagedObjectReference _morHyperHost;
188188

189189
CopyCommand storageCmd;
190190
EnumMap<VmwareStorageProcessorConfigurableFields, Object> params = new EnumMap<VmwareStorageProcessorConfigurableFields,Object>(VmwareStorageProcessorConfigurableFields.class);
@@ -201,11 +201,9 @@ public VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd) {
201201

202202
private Map<String,String> specsArray = new HashMap<String,String>();
203203

204-
AutoCloseable closeable;
205204

206205
@Before
207206
public void setup() throws Exception {
208-
closeable = MockitoAnnotations.openMocks(this);
209207
storageCmd = mock(CopyCommand.class);
210208
doReturn(context).when(_resource).getServiceContext(null);
211209
when(cmd.getVirtualMachine()).thenReturn(vmSpec);
@@ -234,11 +232,6 @@ public void setup() throws Exception {
234232
when(hostCapability.isNestedHVSupported()).thenReturn(true);
235233
}
236234

237-
@After
238-
public void tearDown() throws Exception {
239-
closeable.close();
240-
}
241-
242235
//Test successful scaling up the vm
243236
@Test
244237
public void testScaleVMF1() throws Exception {

0 commit comments

Comments
 (0)