Skip to content

Instantly share code, notes, and snippets.

@karuturi
Last active August 29, 2015 14:25
Show Gist options
  • Save karuturi/ca87782784c1f8b2c24a to your computer and use it in GitHub Desktop.
Save karuturi/ca87782784c1f8b2c24a to your computer and use it in GitHub Desktop.
mocking configKey in unit tests
package org.apache.cloudstack.saml;
import com.cloud.user.DomainManager;
import com.cloud.user.dao.UserDao;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.framework.security.keystore.KeystoreDao;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareEverythingForTest;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.eq;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({SAML2AuthManager.class})
public class SAML2AuthManagerImplTest {
@Mock
private KeystoreDao _ksDao;
@Mock
private SAMLTokenDao _samlTokenDao;
@Mock
private UserDao _userDao;
@Mock
DomainManager _domainMgr;
@Mock
ConfigurationDao _configDao;
@InjectMocks
SAML2AuthManagerImpl saml2AuthManager;
@Before
public void setUp() throws Exception {
}
@Test
public void testStart() throws Exception {
ConfigKey configKey = PowerMockito.mock(ConfigKey.class);
Mockito.when(configKey.value()).thenReturn(true);
PowerMockito.whenNew(ConfigKey.class).withArguments(Mockito.anyString(), eq(Boolean.class), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito
.anyBoolean())
.thenReturn(configKey);
saml2AuthManager.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment