Skip to content

Commit c5677cf

Browse files
committed
CLOUDSTACK-9252: Mock application context for unit test
1 parent 068fcea commit c5677cf

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
import com.cloud.utils.FileUtil;
113113
import com.cloud.utils.NumbersUtil;
114114
import com.cloud.utils.Pair;
115+
import com.cloud.utils.component.ComponentContext;
115116
import com.cloud.utils.component.ManagerBase;
116117
import com.cloud.utils.concurrency.NamedThreadFactory;
117118
import com.cloud.utils.db.DB;
@@ -200,10 +201,12 @@ public class VmwareManagerImpl extends ManagerBase implements VmwareManager, Vmw
200201

201202
private final ScheduledExecutorService _hostScanScheduler = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Vmware-Host-Scan"));
202203

204+
private ApplicationContext applicationContext;
205+
203206
public VmwareManagerImpl() {
204-
_storageMgr = new VmwareStorageManagerImpl(this);
205-
ApplicationContext applicationContext = com.cloud.utils.component.ComponentContext.getApplicationContext();
207+
applicationContext = ComponentContext.getApplicationContext();
206208
imageStoreDetailsUtil = applicationContext.getBean("imageStoreDetailsUtil", ImageStoreDetailsUtil.class);
209+
_storageMgr = new VmwareStorageManagerImpl(this);
207210
}
208211

209212
@Override

plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/VmwareDatacenterApiUnitTest.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.mockito.Mock;
3939
import org.mockito.Mockito;
4040
import org.mockito.MockitoAnnotations;
41+
import org.powermock.core.classloader.annotations.PrepareForTest;
42+
import org.springframework.context.ApplicationContext;
4143
import org.springframework.context.annotation.Bean;
4244
import org.springframework.context.annotation.ComponentScan;
4345
import org.springframework.context.annotation.ComponentScan.Filter;
@@ -54,8 +56,6 @@
5456
import org.apache.cloudstack.context.CallContext;
5557
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
5658
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
57-
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
58-
import org.apache.cloudstack.storage.datastore.db.ImageStoreDetailsDao;
5959
import org.apache.cloudstack.test.utils.SpringUtils;
6060

6161
import com.cloud.agent.AgentManager;
@@ -101,6 +101,7 @@
101101

102102
@RunWith(SpringJUnit4ClassRunner.class)
103103
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
104+
@PrepareForTest({ComponentContext.class, ApplicationContext.class})
104105
public class VmwareDatacenterApiUnitTest {
105106

106107
@Inject
@@ -133,9 +134,6 @@ public class VmwareDatacenterApiUnitTest {
133134
@Inject
134135
AccountManager _acctMgr;
135136

136-
@Inject
137-
ImageStoreDetailsUtil imageStoreDetailsUtil;
138-
139137
long zoneId;
140138
long podId;
141139
long clusterId;
@@ -163,6 +161,13 @@ public class VmwareDatacenterApiUnitTest {
163161

164162
@BeforeClass
165163
public static void setUp() throws ConfigurationException {
164+
ComponentContext ctx = new ComponentContext();
165+
ImageStoreDetailsUtil imgSDUtil = Mockito.mock(ImageStoreDetailsUtil.class);
166+
Mockito.when(imgSDUtil.getNfsVersion(Mockito.anyLong())).thenReturn("3");
167+
Mockito.when(imgSDUtil.getNfsVersionByUuid(Mockito.anyString())).thenReturn("3");
168+
ApplicationContext appCtx = Mockito.mock(ApplicationContext.class);
169+
Mockito.when(appCtx.getBean(Mockito.anyString(), Mockito.any(ImageStoreDetailsUtil.class))).thenReturn(imgSDUtil);
170+
ctx.setApplicationContext(appCtx);
166171
}
167172

168173
@Before
@@ -230,8 +235,6 @@ public void testSetUp() {
230235
Mockito.when(addCmd.getPassword()).thenReturn(password);
231236
Mockito.when(addCmd.getName()).thenReturn(vmwareDcName);
232237
Mockito.when(removeCmd.getZoneId()).thenReturn(1L);
233-
Mockito.when(imageStoreDetailsUtil.getNfsVersion(Mockito.anyLong())).thenReturn(null);
234-
Mockito.when(imageStoreDetailsUtil.getNfsVersionByUuid(Mockito.anyString())).thenReturn(null);
235238
}
236239

237240
@After
@@ -439,21 +442,6 @@ public DataStoreManager dataStoreManager() {
439442
return Mockito.mock(DataStoreManager.class);
440443
}
441444

442-
@Bean
443-
public ImageStoreDetailsUtil imageStoreDetailsUtil() {
444-
return Mockito.mock(ImageStoreDetailsUtil.class);
445-
}
446-
447-
@Bean
448-
public ImageStoreDao imageStoreDao() {
449-
return Mockito.mock(ImageStoreDao.class);
450-
}
451-
452-
@Bean
453-
public ImageStoreDetailsDao imageStoreDetailsDao() {
454-
return Mockito.mock(ImageStoreDetailsDao.class);
455-
}
456-
457445
public static class Library implements TypeFilter {
458446

459447
@Override

0 commit comments

Comments
 (0)