Search in sources :

Example 71 with InvocationOnMock

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

the class GarageServiceTest method shouldFailWhenGitExecutionFailsWithANonZeroReturnCode.

@Test
public void shouldFailWhenGitExecutionFailsWithANonZeroReturnCode() 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(129);
    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(false));
    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)

Example 72 with InvocationOnMock

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

the class DrawableTestUtils method stubSetAlpha.

/**
   * Stubs setAlpha method.
   * @param drawable to stub method of
   */
public static void stubSetAlpha(final Drawable drawable) {
    final AtomicInteger atomicInteger = new AtomicInteger(255);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Integer alpha = (Integer) invocation.getArguments()[0];
            drawable.invalidateSelf();
            atomicInteger.set(alpha);
            return null;
        }
    }).when(drawable).setAlpha(anyInt());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 73 with InvocationOnMock

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

the class DrawableTestUtils method stubGetAndSetBounds.

/**
   * Stubs setBounds and getBounds methods.
   * @param drawable drawable to stub methods of
   */
public static void stubGetAndSetBounds(Drawable drawable) {
    final Rect rect = new Rect();
    when(drawable.getBounds()).thenReturn(rect);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            rect.set((Integer) invocation.getArguments()[0], (Integer) invocation.getArguments()[1], (Integer) invocation.getArguments()[2], (Integer) invocation.getArguments()[3]);
            return null;
        }
    }).when(drawable).setBounds(anyInt(), anyInt(), anyInt(), anyInt());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Rect(android.graphics.Rect) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 74 with InvocationOnMock

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

the class DraweeMocks method stubGetAndSetHierarchy.

/**
   * Stubs setHierarchy and getHierarchy methods.
   * @param controller controller to stub methods of
   */
public static void stubGetAndSetHierarchy(DraweeController controller) {
    final DraweeHierarchy[] dhHolder = new DraweeHierarchy[1];
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return dhHolder[0];
        }
    }).when(controller).getHierarchy();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return dhHolder[0] = (DraweeHierarchy) invocation.getArguments()[0];
        }
    }).when(controller).setHierarchy(any(DraweeHierarchy.class));
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GenericDraweeHierarchy(com.facebook.drawee.generic.GenericDraweeHierarchy) DraweeHierarchy(com.facebook.drawee.interfaces.DraweeHierarchy)

Example 75 with InvocationOnMock

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

the class DraweeMocks method stubGetAndSetContentDescription.

/**
   * Stubs setContentDescription and getContentDescription methods.
   * @param controller controller to stub methods of
   */
public static void stubGetAndSetContentDescription(DraweeController controller) {
    final String[] contentDescriptionHolder = new String[1];
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return contentDescriptionHolder[0];
        }
    }).when(controller).getContentDescription();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return contentDescriptionHolder[0] = (String) invocation.getArguments()[0];
        }
    }).when(controller).setContentDescription(any(String.class));
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

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