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;
}
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;
}
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);
}
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);
}
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));
}
Aggregations