Search in sources :

Example 76 with Answer

use of org.mockito.stubbing.Answer in project fresco by facebook.

the class DraweeMocks method stubControllerListener.

/**
   * Stubs addControllerListener
   * @param controller
   * @return forwarding listener
   */
public static ControllerListener stubControllerListener(final DraweeController controller) {
    final ForwardingControllerListener forwardingListener = new ForwardingControllerListener();
    if (!(controller instanceof AbstractDraweeController)) {
        return null;
    }
    AbstractDraweeController abstractController = (AbstractDraweeController) controller;
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            forwardingListener.addListener((ControllerListener) invocation.getArguments()[0]);
            return null;
        }
    }).when(abstractController).addControllerListener(any(ControllerListener.class));
    return forwardingListener;
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ControllerListener(com.facebook.drawee.controller.ControllerListener) ForwardingControllerListener(com.facebook.drawee.controller.ForwardingControllerListener) ForwardingControllerListener(com.facebook.drawee.controller.ForwardingControllerListener) AbstractDraweeController(com.facebook.drawee.controller.AbstractDraweeController)

Example 77 with Answer

use of org.mockito.stubbing.Answer in project fresco by facebook.

the class DraweeMocks method mockBuilderOf.

/**
   * Creates a mock GenericDraweeHierarchyBuilder with stubbed build.
   * @param drawableHierarchies drawable hierarchies to return on {@code build()}
   * @return mock GenericDraweeHierarchyBuilder
   */
public static GenericDraweeHierarchyBuilder mockBuilderOf(GenericDraweeHierarchy... drawableHierarchies) {
    GenericDraweeHierarchyBuilder builder = mock(GenericDraweeHierarchyBuilder.class, CALLS_REAL_METHODS);
    final Supplier<GenericDraweeHierarchy> gdhProvider = supplierOf(drawableHierarchies);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return gdhProvider.get();
        }
    }).when(builder).build();
    return builder;
}
Also used : Answer(org.mockito.stubbing.Answer) GenericDraweeHierarchyBuilder(com.facebook.drawee.generic.GenericDraweeHierarchyBuilder) GenericDraweeHierarchy(com.facebook.drawee.generic.GenericDraweeHierarchy) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 78 with Answer

use of org.mockito.stubbing.Answer in project gocd by gocd.

the class PluggableTaskPreferenceLoaderTest method shouldSetConfigForTheTaskCorrespondingToGivenPluginId.

@Test
public void shouldSetConfigForTheTaskCorrespondingToGivenPluginId() throws Exception {
    final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class);
    String pluginId = "test-plugin-id";
    when(descriptor.id()).thenReturn(pluginId);
    final Task task = mock(Task.class);
    TaskConfig config = new TaskConfig();
    TaskView taskView = mock(TaskView.class);
    when(task.config()).thenReturn(config);
    when(task.view()).thenReturn(taskView);
    PluginManager pluginManager = mock(PluginManager.class);
    final TaskExtension taskExtension = mock(TaskExtension.class);
    when(taskExtension.canHandlePlugin(pluginId)).thenReturn(true);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            final Action<Task> action = (Action<Task>) invocationOnMock.getArguments()[1];
            action.execute(task, descriptor);
            return null;
        }
    }).when(taskExtension).doOnTask(eq(pluginId), any(Action.class));
    when(pluginManager.hasReferenceFor(Task.class, pluginId)).thenReturn(true);
    when(pluginManager.isPluginOfType("task-plugin", pluginId)).thenReturn(false);
    PluggableTaskPreferenceLoader pluggableTaskPreferenceLoader = new PluggableTaskPreferenceLoader(pluginManager, taskExtension);
    pluggableTaskPreferenceLoader.pluginLoaded(descriptor);
    assertThat(PluggableTaskConfigStore.store().hasPreferenceFor(pluginId), is(true));
    assertThat(PluggableTaskConfigStore.store().preferenceFor(pluginId), is(new TaskPreference(task)));
    verify(pluginManager).addPluginChangeListener(pluggableTaskPreferenceLoader, Task.class, GoPlugin.class);
}
Also used : Task(com.thoughtworks.go.plugin.api.task.Task) TaskView(com.thoughtworks.go.plugin.api.task.TaskView) Action(com.thoughtworks.go.plugin.infra.Action) TaskConfig(com.thoughtworks.go.plugin.api.task.TaskConfig) PluginManager(com.thoughtworks.go.plugin.infra.PluginManager) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) Test(org.junit.Test)

Example 79 with Answer

use of org.mockito.stubbing.Answer in project gocd by gocd.

the class PluggableTaskPreferenceLoaderTest method shouldLoadPreferencesOnlyForTaskPlugins.

@Test
public void shouldLoadPreferencesOnlyForTaskPlugins() {
    final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class);
    String pluginId = "test-plugin-id";
    when(descriptor.id()).thenReturn(pluginId);
    final Task task = mock(Task.class);
    TaskConfig config = new TaskConfig();
    TaskView taskView = mock(TaskView.class);
    when(task.config()).thenReturn(config);
    when(task.view()).thenReturn(taskView);
    PluginManager pluginManager = mock(PluginManager.class);
    final TaskExtension taskExtension = mock(TaskExtension.class);
    when(taskExtension.canHandlePlugin(pluginId)).thenReturn(false);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            final Action<Task> action = (Action<Task>) invocationOnMock.getArguments()[1];
            action.execute(task, descriptor);
            return null;
        }
    }).when(taskExtension).doOnTask(eq(pluginId), any(Action.class));
    PluggableTaskPreferenceLoader pluggableTaskPreferenceLoader = new PluggableTaskPreferenceLoader(pluginManager, taskExtension);
    pluggableTaskPreferenceLoader.pluginLoaded(descriptor);
    assertThat(PluggableTaskConfigStore.store().hasPreferenceFor(pluginId), is(false));
    verify(pluginManager).addPluginChangeListener(pluggableTaskPreferenceLoader, Task.class, GoPlugin.class);
}
Also used : Task(com.thoughtworks.go.plugin.api.task.Task) TaskView(com.thoughtworks.go.plugin.api.task.TaskView) Action(com.thoughtworks.go.plugin.infra.Action) TaskConfig(com.thoughtworks.go.plugin.api.task.TaskConfig) PluginManager(com.thoughtworks.go.plugin.infra.PluginManager) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) Test(org.junit.Test)

Example 80 with Answer

use of org.mockito.stubbing.Answer in project gocd by gocd.

the class JsonBasedTaskExecutorTest method shouldConstructExecutionRequestWithRequiredDetails.

@Test
public void shouldConstructExecutionRequestWithRequiredDetails() {
    String workingDir = "working-dir";
    com.thoughtworks.go.plugin.api.task.Console console = mock(com.thoughtworks.go.plugin.api.task.Console.class);
    when(context.workingDir()).thenReturn(workingDir);
    EnvironmentVariables environment = getEnvironmentVariables();
    when(context.environment()).thenReturn(environment);
    when(context.console()).thenReturn(console);
    final GoPluginApiRequest[] executionRequest = new GoPluginApiRequest[1];
    when(response.responseBody()).thenReturn("{\"success\":true,\"messages\":[\"message1\",\"message2\"]}");
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            GoPluginApiRequest request = (GoPluginApiRequest) invocationOnMock.getArguments()[1];
            executionRequest[0] = request;
            return response;
        }
    }).when(pluginManager).submitTo(eq(pluginId), any(GoPluginApiRequest.class));
    handler = new JsonBasedTaskExtensionHandler_V1();
    handlerHashMap.put("1.0", handler);
    new JsonBasedTaskExecutor(pluginId, pluginRequestHelper, handlerHashMap).execute(config(), context);
    assertTrue(executionRequest.length == 1);
    Map result = (Map) new GsonBuilder().create().fromJson(executionRequest[0].requestBody(), Object.class);
    Map context = (Map) result.get("context");
    assertThat(context.get("workingDirectory"), is(workingDir));
    Map environmentVariables = (Map) context.get("environmentVariables");
    assertThat(environmentVariables.size(), is(2));
    assertThat(environmentVariables.get("ENV1").toString(), is("VAL1"));
    assertThat(environmentVariables.get("ENV2").toString(), is("VAL2"));
    assertThat(executionRequest[0].requestParameters().size(), is(0));
}
Also used : GoPluginApiRequest(com.thoughtworks.go.plugin.api.request.GoPluginApiRequest) GsonBuilder(com.google.gson.GsonBuilder) Answer(org.mockito.stubbing.Answer) com.thoughtworks.go.plugin.api.task(com.thoughtworks.go.plugin.api.task) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Answer (org.mockito.stubbing.Answer)262 InvocationOnMock (org.mockito.invocation.InvocationOnMock)247 Test (org.junit.Test)148 Mockito.doAnswer (org.mockito.Mockito.doAnswer)99 Before (org.junit.Before)36 Matchers.anyString (org.mockito.Matchers.anyString)32 HashMap (java.util.HashMap)31 ArrayList (java.util.ArrayList)30 IOException (java.io.IOException)20 HashSet (java.util.HashSet)16 File (java.io.File)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 Map (java.util.Map)13 List (java.util.List)12 Test (org.testng.annotations.Test)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 Configuration (org.apache.hadoop.conf.Configuration)11 RequestFinishedListener (com.android.volley.RequestQueue.RequestFinishedListener)9 MockRequest (com.android.volley.mock.MockRequest)9