Search in sources :

Example 66 with Mock

use of org.mockito.Mock in project flue2ent by DefinityLabs.

the class WaiterPluginTest method expectedCondition_until_callsWaitUntil.

@Test
public void expectedCondition_until_callsWaitUntil() {
    WaiterPlugin waiter = new WaiterPlugin(website);
    WebElement mockedElement = mock(WebElement.class);
    when(wait.until(any())).thenReturn(mockedElement);
    ExpectedCondition<WebElement> expectedCondition = driver -> mockedElement;
    WebElement element = waiter.until(expectedCondition);
    verify(wait).until(same(expectedCondition));
    assertThat(element).isSameAs(mockedElement);
}
Also used : Arrays(java.util.Arrays) Matchers.same(org.mockito.Matchers.same) Mock(org.mockito.Mock) WebDriver(org.openqa.selenium.WebDriver) RunWith(org.junit.runner.RunWith) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) WebElement(org.openqa.selenium.WebElement) Test(org.junit.Test) Function(java.util.function.Function) Supplier(java.util.function.Supplier) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) FluentWait(org.openqa.selenium.support.ui.FluentWait) Mockito(org.mockito.Mockito) SeleniumElementCreator(org.definitylabs.flue2ent.element.SeleniumElementCreator) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Website(org.definitylabs.flue2ent.Website) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) Before(org.junit.Before) WebElement(org.openqa.selenium.WebElement) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 67 with Mock

use of org.mockito.Mock in project smarthome by eclipse.

the class HomeAssistantMQTTImplementationTests method parseHATree.

@Test
public void parseHATree() throws InterruptedException, ExecutionException, TimeoutException {
    MqttChannelTypeProvider channelTypeProvider = mock(MqttChannelTypeProvider.class);
    final Map<String, AbstractComponent> haComponents = new HashMap<String, AbstractComponent>();
    ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(4);
    DiscoverComponents discover = spy(new DiscoverComponents(ThingChannelConstants.testHomeAssistantThing, scheduler, channelStateUpdateListener, new Gson()));
    // The DiscoverComponents object calls ComponentDiscovered callbacks.
    // In the following implementation we add the found component to the `haComponents` map
    // and add the types to the channelTypeProvider, like in the real Thing handler.
    final CountDownLatch latch = new CountDownLatch(1);
    ComponentDiscovered cd = (haID, c) -> {
        haComponents.put(haID.getChannelGroupID(), c);
        c.addChannelTypes(channelTypeProvider);
        channelTypeProvider.setChannelGroupType(c.groupTypeUID(), c.type());
        latch.countDown();
    };
    // Start the discovery for 100ms. Forced timeout after 300ms.
    HaID haID = new HaID(testObjectTopic);
    CompletableFuture<Void> future = discover.startDiscovery(connection, 100, haID, cd).thenRun(() -> {
    }).exceptionally(e -> {
        failure = e;
        return null;
    });
    assertTrue(latch.await(300, TimeUnit.MILLISECONDS));
    future.get(100, TimeUnit.MILLISECONDS);
    // No failure expected and one discoverd result
    assertNull(failure);
    assertThat(haComponents.size(), is(1));
    // For the switch component we should have one channel group type and one channel type
    verify(channelTypeProvider, times(1)).setChannelGroupType(any(), any());
    verify(channelTypeProvider, times(1)).setChannelType(any(), any());
    // We expect a switch component with an OnOff channel with the initial value UNDEF:
    State value = haComponents.get(haID.getChannelGroupID()).channelTypes().get(ComponentSwitch.switchChannelID).channelState.getCache().getChannelState();
    assertThat(value, is(UnDefType.UNDEF));
    haComponents.values().stream().map(e -> e.start(connection, scheduler, 100)).reduce(CompletableFuture.completedFuture(null), (a, v) -> a.thenCompose(b -> v)).exceptionally(e -> {
        failure = e;
        return null;
    }).get();
    // We should have received the retained value, while subscribing to the channels MQTT state topic.
    verify(channelStateUpdateListener, times(1)).updateChannelState(any(), any());
    // Value should be ON now.
    value = haComponents.get(haID.getChannelGroupID()).channelTypes().get(ComponentSwitch.switchChannelID).channelState.getCache().getChannelState();
    assertThat(value, is(OnOffType.ON));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) Mock(org.mockito.Mock) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) UnDefType(org.eclipse.smarthome.core.types.UnDefType) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MqttConnectionState(org.eclipse.smarthome.io.transport.mqtt.MqttConnectionState) DiscoverComponents(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.DiscoverComponents) AbstractComponent(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.AbstractComponent) ArrayList(java.util.ArrayList) MqttService(org.eclipse.smarthome.io.transport.mqtt.MqttService) ConfigurationException(org.osgi.service.cm.ConfigurationException) Nullable(org.eclipse.jdt.annotation.Nullable) Gson(com.google.gson.Gson) Map(java.util.Map) After(org.junit.After) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ChannelStateUpdateListener(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.ChannelStateUpdateListener) State(org.eclipse.smarthome.core.types.State) MqttConnectionObserver(org.eclipse.smarthome.io.transport.mqtt.MqttConnectionObserver) MqttChannelTypeProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelTypeProvider) ComponentSwitch(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.ComponentSwitch) Before(org.junit.Before) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) ComponentDiscovered(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.DiscoverComponents.ComponentDiscovered) Logger(org.slf4j.Logger) ThingChannelConstants(org.eclipse.smarthome.binding.mqtt.generic.internal.handler.ThingChannelConstants) MqttBrokerConnection(org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection) HaID(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.HaID) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert(org.junit.Assert) NonNull(org.eclipse.jdt.annotation.NonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MqttChannelTypeProvider(org.eclipse.smarthome.binding.mqtt.generic.internal.generic.MqttChannelTypeProvider) HashMap(java.util.HashMap) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Gson(com.google.gson.Gson) CountDownLatch(java.util.concurrent.CountDownLatch) DiscoverComponents(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.DiscoverComponents) AbstractComponent(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.AbstractComponent) ComponentDiscovered(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.DiscoverComponents.ComponentDiscovered) HaID(org.eclipse.smarthome.binding.mqtt.generic.internal.convention.homeassistant.HaID) MqttConnectionState(org.eclipse.smarthome.io.transport.mqtt.MqttConnectionState) State(org.eclipse.smarthome.core.types.State) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 68 with Mock

use of org.mockito.Mock in project checkstyle-idea by jshiell.

the class RelativeFileConfigurationLocationTest method setUp.

@Before
public void setUp() {
    ProjectPaths projectPaths = mock(ProjectPaths.class);
    Function<File, String> absolutePathOf = file -> {
        // a nasty hack to pretend we're on a Windows box when required...
        if (file.getPath().startsWith("c:")) {
            return file.getPath().replace('/', '\\').replaceAll("\\\\\\\\", "\\\\");
        }
        return FilenameUtils.separatorsToUnix(file.getPath());
    };
    ProjectFilePaths testProjectFilePaths = new ProjectFilePaths(project, '/', absolutePathOf, projectPaths);
    when(project.getService(ProjectFilePaths.class)).thenReturn(testProjectFilePaths);
    when(projectBase.getPath()).thenReturn(PROJECT_BASE_PATH);
    when(projectPaths.projectPath(project)).thenReturn(projectBase);
    underTest = new RelativeFileConfigurationLocation(project);
    underTest.setLocation("aLocation");
    underTest.setDescription("aDescription");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) ProjectPaths(org.infernus.idea.checkstyle.util.ProjectPaths) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) File(java.io.File) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Project(com.intellij.openapi.project.Project) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) FilenameUtils(org.apache.commons.io.FilenameUtils) ProjectFilePaths(org.infernus.idea.checkstyle.util.ProjectFilePaths) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) ProjectPaths(org.infernus.idea.checkstyle.util.ProjectPaths) ProjectFilePaths(org.infernus.idea.checkstyle.util.ProjectFilePaths) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Before(org.junit.Before)

Example 69 with Mock

use of org.mockito.Mock in project datashare by ICIJ.

the class BatchDownloadRunnerEncryptedIntTest method test_zip_with_password_should_encrypt_file_and_send_mail.

@Test
public void test_zip_with_password_should_encrypt_file_and_send_mail() throws Exception {
    new IndexerHelper(es.client).indexFile("mydoc.txt", "content", fs);
    BatchDownload batchDownload = createBatchDownload("*");
    MailSender mailSender = mock(MailSender.class);
    new BatchDownloadRunner(indexer, createProvider(), batchDownload, updateCallback, (uri) -> mailSender).call();
    assertThat(new net.lingala.zip4j.ZipFile(batchDownload.filename.toFile()).isEncrypted()).isTrue();
    ArgumentCaptor<Mail> mailCaptor = ArgumentCaptor.forClass(Mail.class);
    verify(mailSender).send(mailCaptor.capture());
    assertThat(mailCaptor.getValue().from).isEqualTo("engineering@icij.org");
    assertThat(mailCaptor.getValue().toRecipientList).containsExactly("foo@bar.com");
    assertThat(mailCaptor.getValue().subject).isEqualTo("[datashare] " + batchDownload.filename.getFileName());
}
Also used : ElasticsearchRule(org.icij.datashare.test.ElasticsearchRule) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) PropertiesProvider(org.icij.datashare.PropertiesProvider) ElasticsearchIndexer(org.icij.datashare.text.indexing.elasticsearch.ElasticsearchIndexer) Mock(org.mockito.Mock) IOException(java.io.IOException) HashMap(java.util.HashMap) TEST_INDEX(org.icij.datashare.test.ElasticsearchRule.TEST_INDEX) Function(java.util.function.Function) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThat(org.fest.assertions.Assertions.assertThat) MailSender(org.icij.datashare.com.mail.MailSender) User(org.icij.datashare.user.User) Project.project(org.icij.datashare.text.Project.project) org.junit(org.junit) IMMEDIATE(org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE) BatchDownload(org.icij.datashare.batch.BatchDownload) NotNull(org.jetbrains.annotations.NotNull) Mail(org.icij.datashare.com.mail.Mail) TemporaryFolder(org.junit.rules.TemporaryFolder) Mockito.mock(org.mockito.Mockito.mock) BatchDownload(org.icij.datashare.batch.BatchDownload) Mail(org.icij.datashare.com.mail.Mail) MailSender(org.icij.datashare.com.mail.MailSender)

Example 70 with Mock

use of org.mockito.Mock in project android_packages_apps_Settings by omnirom.

the class HibernatedAppsPreferenceControllerTest method setUp.

@Before
public void setUp() {
    if (Looper.myLooper() == null) {
        Looper.prepare();
    }
    MockitoAnnotations.initMocks(this);
    DeviceConfig.setProperty(NAMESPACE_APP_HIBERNATION, PROPERTY_APP_HIBERNATION_ENABLED, "true", false);
    mContext = spy(ApplicationProvider.getApplicationContext());
    when(mContext.getPackageManager()).thenReturn(mPackageManager);
    when(mContext.getSystemService(AppHibernationManager.class)).thenReturn(mAppHibernationManager);
    when(mContext.getSystemService(UsageStatsManager.class)).thenReturn(new UsageStatsManager(mContext, mIUsageStatsManager));
    PreferenceManager manager = new PreferenceManager(mContext);
    mPreferenceScreen = manager.createPreferenceScreen(mContext);
    Preference preference = mock(Preference.class);
    when(preference.getKey()).thenReturn(KEY);
    mPreferenceScreen.addPreference(preference);
    mController = new HibernatedAppsPreferenceController(mContext, KEY, command -> command.run(), command -> command.run());
}
Also used : Context(android.content.Context) Arrays(java.util.Arrays) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) PackageManager(android.content.pm.PackageManager) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) AppHibernationManager(android.apphibernation.AppHibernationManager) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) RemoteException(android.os.RemoteException) Mockito.spy(org.mockito.Mockito.spy) PackageInfo(android.content.pm.PackageInfo) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) AVAILABLE(com.android.settings.core.BasePreferenceController.AVAILABLE) ApplicationProvider(androidx.test.core.app.ApplicationProvider) IUsageStatsManager(android.app.usage.IUsageStatsManager) MockitoAnnotations(org.mockito.MockitoAnnotations) UsageStats(android.app.usage.UsageStats) Looper(android.os.Looper) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Before(org.junit.Before) PreferenceScreen(androidx.preference.PreferenceScreen) ParceledListSlice(android.content.pm.ParceledListSlice) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Preference(androidx.preference.Preference) Mockito.verify(org.mockito.Mockito.verify) UsageStatsManager(android.app.usage.UsageStatsManager) NAMESPACE_APP_HIBERNATION(android.provider.DeviceConfig.NAMESPACE_APP_HIBERNATION) PreferenceManager(androidx.preference.PreferenceManager) DeviceConfig(android.provider.DeviceConfig) PROPERTY_APP_HIBERNATION_ENABLED(com.android.settings.Utils.PROPERTY_APP_HIBERNATION_ENABLED) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Resources(android.content.res.Resources) Preference(androidx.preference.Preference) IUsageStatsManager(android.app.usage.IUsageStatsManager) UsageStatsManager(android.app.usage.UsageStatsManager) PreferenceManager(androidx.preference.PreferenceManager) Before(org.junit.Before)

Aggregations

Mock (org.mockito.Mock)135 Mockito.mock (org.mockito.Mockito.mock)109 Test (org.junit.Test)104 Before (org.junit.Before)96 Mockito.verify (org.mockito.Mockito.verify)89 Mockito.when (org.mockito.Mockito.when)82 List (java.util.List)81 RunWith (org.junit.runner.RunWith)76 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)65 Collections (java.util.Collections)56 ArrayList (java.util.ArrayList)55 ArgumentCaptor (org.mockito.ArgumentCaptor)52 Mockito.spy (org.mockito.Mockito.spy)48 Arrays (java.util.Arrays)44 Assert.assertEquals (org.junit.Assert.assertEquals)41 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)41 Mockito.doReturn (org.mockito.Mockito.doReturn)41 Map (java.util.Map)40 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)38 Mockito.never (org.mockito.Mockito.never)37