Search in sources :

Example 66 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project spring-security by spring-projects.

the class UsernamePasswordAuthenticationFilterTests method createAuthenticationManager.

private AuthenticationManager createAuthenticationManager() {
    AuthenticationManager am = mock(AuthenticationManager.class);
    when(am.authenticate(any(Authentication.class))).thenAnswer(new Answer<Authentication>() {

        public Authentication answer(InvocationOnMock invocation) throws Throwable {
            return (Authentication) invocation.getArguments()[0];
        }
    });
    return am;
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) Authentication(org.springframework.security.core.Authentication) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 67 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project ExoPlayer by google.

the class CronetDataSourceTest method testCallbackFromPreviousRequest.

@Test
public void testCallbackFromPreviousRequest() throws HttpDataSourceException {
    mockResponseStartSuccess();
    dataSourceUnderTest.open(testDataSpec);
    dataSourceUnderTest.close();
    // Prepare a mock UrlRequest to be used in the second open() call.
    final UrlRequest mockUrlRequest2 = mock(UrlRequest.class);
    when(mockUrlRequestBuilder.build()).thenReturn(mockUrlRequest2);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            // Invoke the callback for the previous request.
            dataSourceUnderTest.onFailed(mockUrlRequest, testUrlResponseInfo, mockNetworkException);
            dataSourceUnderTest.onResponseStarted(mockUrlRequest2, testUrlResponseInfo);
            return null;
        }
    }).when(mockUrlRequest2).start();
    dataSourceUnderTest.open(testDataSpec);
}
Also used : UrlRequest(org.chromium.net.UrlRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 68 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project gocd by gocd.

the class DefaultPluginManagerTest method shouldNotFindPluginIsOfGivenExtensionWhenReferenceNotFoundAndExtensionDoNotMatch.

@Test
public void shouldNotFindPluginIsOfGivenExtensionWhenReferenceNotFoundAndExtensionDoNotMatch() throws Exception {
    final String pluginThatDoesNotImplement = "plugin-that-does-not-implement";
    GoPluginIdentifier pluginIdentifier = new GoPluginIdentifier("another-extension-type", asList("1.0"));
    final GoPlugin goPlugin = mock(GoPlugin.class);
    final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class);
    when(goPluginOSGiFramework.hasReferenceFor(GoPlugin.class, pluginThatDoesNotImplement)).thenReturn(true);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ActionWithReturn<GoPlugin, GoPluginApiResponse> action = (ActionWithReturn<GoPlugin, GoPluginApiResponse>) invocationOnMock.getArguments()[2];
            return action.execute(goPlugin, descriptor);
        }
    }).when(goPluginOSGiFramework).doOn(eq(GoPlugin.class), eq(pluginThatDoesNotImplement), any(ActionWithReturn.class));
    when(goPlugin.pluginIdentifier()).thenReturn(pluginIdentifier);
    DefaultPluginManager pluginManager = new DefaultPluginManager(monitor, registry, goPluginOSGiFramework, jarChangeListener, pluginRequestProcessorRegistry, pluginWriter, pluginValidator, systemEnvironment, pluginsZipUpdater, pluginsListListener);
    assertFalse(pluginManager.isPluginOfType("extension-type", pluginThatDoesNotImplement));
    verify(goPluginOSGiFramework).doOn(eq(GoPlugin.class), eq(pluginThatDoesNotImplement), any(ActionWithReturn.class));
}
Also used : GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) Test(org.junit.Test)

Example 69 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project stetho by facebook.

the class StethoInterceptorTest method hookAlmostRealInterpretResponseStream.

/**
   * Provide a suitably "real" implementation of
   * {@link NetworkEventReporter#interpretResponseStream} for our mock to test that
   * events are properly delegated.
   */
private static ByteArrayOutputStream hookAlmostRealInterpretResponseStream(final NetworkEventReporter mockEventReporter) {
    final ByteArrayOutputStream capturedOutput = new ByteArrayOutputStream();
    Mockito.when(mockEventReporter.interpretResponseStream(anyString(), anyString(), anyString(), any(InputStream.class), any(ResponseHandler.class))).thenAnswer(new Answer<InputStream>() {

        @Override
        public InputStream answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            String requestId = (String) args[0];
            String contentEncoding = (String) args[2];
            InputStream responseStream = (InputStream) args[3];
            ResponseHandler responseHandler = (ResponseHandler) args[4];
            return DecompressionHelper.teeInputWithDecompression(null, /* networkPeerManager */
            requestId, responseStream, capturedOutput, contentEncoding, responseHandler);
        }
    });
    return capturedOutput;
}
Also used : ResponseHandler(com.facebook.stetho.inspector.network.ResponseHandler) InputStream(java.io.InputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 70 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project gocd by gocd.

the class GarageServiceTest method shouldSucceedWhenGitGCIsCompleted.

@Test
public void shouldSucceedWhenGitGCIsCompleted() throws Exception {
    CommandLine firstCommand = mock(CommandLine.class);
    doReturn(firstCommand).when(firstCommand).withArg(anyString());
    doReturn(firstCommand).when(firstCommand).withWorkingDir(any(File.class));
    when(firstCommand.run(any(InMemoryStreamConsumer.class), eq(GarageService.PROCESS_TAG))).thenReturn(0);
    CommandLine secondCommand = mock(CommandLine.class);
    doReturn(secondCommand).when(secondCommand).withArg(anyString());
    doReturn(secondCommand).when(secondCommand).withWorkingDir(any(File.class));
    when(secondCommand.run(any(InMemoryStreamConsumer.class), eq(GarageService.PROCESS_TAG))).thenReturn(0);
    final List<CommandLine> commands = new LinkedList<>();
    commands.add(firstCommand);
    commands.add(secondCommand);
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    GarageService spy = spy(garageService);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            CommandLine remove = commands.get(0);
            commands.remove(remove);
            return remove;
        }
    }).when(spy).getGit();
    spy.gc(result);
    assertThat(result.isSuccessful(), is(true));
    assertThat(result.hasMessage(), is(true));
    verify(spy, times(2)).getGit();
    verify(firstCommand).run(any(InMemoryStreamConsumer.class), eq(GarageService.PROCESS_TAG));
    verify(secondCommand).run(any(InMemoryStreamConsumer.class), eq(GarageService.PROCESS_TAG));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Answer(org.mockito.stubbing.Answer) CommandLine(com.thoughtworks.go.util.command.CommandLine) InvocationOnMock(org.mockito.invocation.InvocationOnMock) InMemoryStreamConsumer(com.thoughtworks.go.util.command.InMemoryStreamConsumer) File(java.io.File) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

InvocationOnMock (org.mockito.invocation.InvocationOnMock)1088 Test (org.junit.Test)655 Answer (org.mockito.stubbing.Answer)287 Matchers.anyString (org.mockito.Matchers.anyString)145 HashMap (java.util.HashMap)124 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)113 Before (org.junit.Before)110 Mockito.doAnswer (org.mockito.Mockito.doAnswer)110 ArrayList (java.util.ArrayList)109 IOException (java.io.IOException)92 Context (android.content.Context)68 List (java.util.List)62 AtomicReference (java.util.concurrent.atomic.AtomicReference)61 CountDownLatch (java.util.concurrent.CountDownLatch)59 Test (org.testng.annotations.Test)59 File (java.io.File)55 UUID (java.util.UUID)46 Configuration (org.apache.hadoop.conf.Configuration)46 Activity (android.app.Activity)40 Semaphore (java.util.concurrent.Semaphore)38