Java Code Examples for org.mockito.invocation.InvocationOnMock#getMethod()

The following examples show how to use org.mockito.invocation.InvocationOnMock#getMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComponentOverrider.java    From DaggerMock with Apache License 2.0 6 votes vote down vote up
public <T> T override(Class<T> componentClass, final Object component, final Map<Class<?>, DaggerMockRule.ObjectDecorator<?>> decorators) {
    Answer defaultAnswer = new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Method method = invocation.getMethod();
            Object[] arguments = invocation.getArguments();
            if (ReflectUtils.isSubComponent(method.getReturnType()) || ReflectUtils.isSubComponentBuilder(method.getReturnType())) {
                Object[] mockedArguments = new Object[arguments.length];
                for (int i = 0; i < arguments.length; i++) {
                    mockedArguments[i] = moduleOverrider.override(arguments[i], decorators);
                }
                arguments = mockedArguments;
                Object originalSubComponent = method.invoke(component, arguments);
                return override(method.getReturnType(), originalSubComponent, decorators);
            } else {
                return method.invoke(component, arguments);
            }
        }
    };
    return Mockito.mock(componentClass, defaultAnswer);
}
 
Example 2
Source File: ModuleOverrider.java    From DaggerMock with Apache License 2.0 6 votes vote down vote up
public <T> T override(final T module, final Map<Class<?>, DaggerMockRule.ObjectDecorator<?>> decorators) {
    checkMethodsVisibility(module);
    Answer defaultAnswer = new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Method method = invocation.getMethod();
            Provider provider = overriddenObjectsMap.getProvider(method);
            Object ret;
            if (provider != null) {
                ret = provider.get();
            } else {
                method.setAccessible(true);
                ret = method.invoke(module, invocation.getArguments());
            }
            DaggerMockRule.ObjectDecorator<Object> decorator =
                    (DaggerMockRule.ObjectDecorator<Object>) decorators.get(method.getReturnType());
            if (decorator != null) {
                ret = decorator.decorate(ret);
            }
            return ret;
        }
    };
    return (T) Mockito.mock(module.getClass(), defaultAnswer);
}
 
Example 3
Source File: CallsRealOrExceptionAnswer.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object answer(final InvocationOnMock invocation) throws Throwable {
    if (Modifier.isAbstract(invocation.getMethod().getModifiers())) {
        throw new UnstubbedMethodException(invocation.getMethod(), invocation.getMock());
    }
    return invocation.callRealMethod();
}
 
Example 4
Source File: TestTaskCommunicatorManager.java    From tez with Apache License 2.0 5 votes vote down vote up
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
  Method method = invocation.getMethod();
  if (method.getDeclaringClass().equals(TaskCommunicator.class) &&
      !method.getName().equals("getContext") && !method.getName().equals("initialize") &&
      !method.getName().equals("start") && !method.getName().equals("shutdown")) {
    throw new RuntimeException("TestException_" + method.getName());
  } else {
    return invocation.callRealMethod();
  }
}
 
Example 5
Source File: TestTaskSchedulerManager.java    From tez with Apache License 2.0 5 votes vote down vote up
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
  Method method = invocation.getMethod();
  if (method.getDeclaringClass().equals(TaskScheduler.class) &&
      !method.getName().equals("getContext") && !method.getName().equals("initialize") &&
      !method.getName().equals("start") && !method.getName().equals("shutdown")) {
    throw new RuntimeException("TestException_" + method.getName());
  } else {
    return invocation.callRealMethod();
  }
}
 
Example 6
Source File: ExcelWriterTest.java    From hop with Apache License 2.0 4 votes vote down vote up
@Override
public Object answer( InvocationOnMock invocation ) throws Throwable {
  throw new RuntimeException( "This method (" + invocation.getMethod() + ") shouldn't have been called." );
}
 
Example 7
Source File: ExcelWriterTransformTest.java    From hop with Apache License 2.0 4 votes vote down vote up
@Override
public Object answer( InvocationOnMock invocation ) throws Throwable {
  throw new RuntimeException( "This method (" + invocation.getMethod() + ") shouldn't have been called." );
}
 
Example 8
Source File: ForwardsInvocations.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public Object answer(InvocationOnMock invocation) throws Throwable {
	Method method = invocation.getMethod() ;

       return method.invoke(delegatedObject, invocation.getArguments());
}
 
Example 9
Source File: ThrowsMethodExceptionAnswer.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Void answer(final InvocationOnMock invocation) {
    throw new UnstubbedMethodException(invocation.getMethod());
}
 
Example 10
Source File: AudioStreamManagerTest.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void testStartAudioStreamManager() {
    final SdlSession mockSession = mock(SdlSession.class);

    Answer<Void> audioServiceAnswer = new Answer<Void>() {
        ISdlServiceListener serviceListener = null;
        @Override
        public Void answer(InvocationOnMock invocation) {
            Method method = invocation.getMethod();
            Object[] args = invocation.getArguments();

            switch (method.getName()) {
                case "addServiceListener":
                    // parameters (SessionType serviceType, ISdlServiceListener sdlServiceListener);
                    SessionType sessionType = (SessionType) args[0];
                    assertEquals(sessionType, SessionType.PCM);
                    serviceListener = (ISdlServiceListener) args[1];
                    break;
                case "startAudioService":
                    // parameters (boolean encrypted, AudioStreamingCodec codec, AudioStreamingParams params);
                    Boolean encrypted = (Boolean) args[0];
                    serviceListener.onServiceStarted(mockSession, SessionType.PCM, encrypted);
                    break;
                case "stopAudioService":
                    // parameters ()
                    serviceListener.onServiceEnded(mockSession, SessionType.PCM);
                    break;
            }

            return null;
        }
    };

    ISdl internalInterface = mock(ISdl.class);
    AudioPassThruCapabilities audioCapabilities = new AudioPassThruCapabilities(SamplingRate._16KHZ, BitsPerSample._16_BIT, AudioType.PCM);
    doReturn(true).when(internalInterface).isConnected();
    doReturn(audioCapabilities).when(internalInterface).getCapability(SystemCapabilityType.PCM_STREAMING);
    doAnswer(audioServiceAnswer).when(internalInterface).addServiceListener(any(SessionType.class), any(ISdlServiceListener.class));
    doAnswer(audioServiceAnswer).when(internalInterface).startAudioService(any(Boolean.class));
    doAnswer(audioServiceAnswer).when(internalInterface).stopAudioService();

    CompletionListener completionListener = new CompletionListener() {
        @Override
        public void onComplete(boolean success) {
            assertEquals(true, success);
        }
    };

    CompletionListener mockListener = spy(completionListener);
    AudioStreamManager manager = new AudioStreamManager(internalInterface, mContext);

    manager.startAudioStream(false, mockListener);
    manager.stopAudioStream(mockListener);
    verify(mockListener, timeout(10000).times(2)).onComplete(any(Boolean.class));
}
 
Example 11
Source File: AudioStreamManagerTest.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void testPlayRawAudio() {
    AudioPassThruCapabilities audioCapabilities = new AudioPassThruCapabilities(SamplingRate._16KHZ, BitsPerSample._16_BIT, AudioType.PCM);

    IAudioStreamListener audioStreamListener = mock(IAudioStreamListener.class);


    final CompletionListener completionListener = mock(CompletionListener.class);

    final SdlSession mockSession = mock(SdlSession.class);
    doReturn(audioStreamListener).when(mockSession).startAudioStream();


    Answer<Void> audioServiceAnswer = new Answer<Void>() {
        ISdlServiceListener serviceListener = null;

        @Override
        public Void answer(InvocationOnMock invocation) {
            Method method = invocation.getMethod();
            Object[] args = invocation.getArguments();

            switch (method.getName()) {
                case "addServiceListener":
                    SessionType sessionType = (SessionType) args[0];
                    assertEquals(sessionType, SessionType.PCM);

                    serviceListener = (ISdlServiceListener) args[1];
                    break;
                case "startAudioService":
                    Boolean encrypted = (Boolean) args[0];
                    serviceListener.onServiceStarted(mockSession, SessionType.PCM, encrypted);
                    break;
            }

            return null;
        }
    };

    ISdl internalInterface = mock(ISdl.class);
    doReturn(true).when(internalInterface).isConnected();
    doReturn(audioCapabilities).when(internalInterface).getCapability(any(SystemCapabilityType.class));
    doAnswer(audioServiceAnswer).when(internalInterface).addServiceListener(any(SessionType.class), any(ISdlServiceListener.class));
    doAnswer(audioServiceAnswer).when(internalInterface).startAudioService(any(Boolean.class));

    final AudioStreamManager manager = new AudioStreamManager(internalInterface, mContext);
    manager.startAudioStream(false, new CompletionListener() {
        @Override
        public void onComplete(boolean success) {
            assertTrue(success);
            byte[] buffer = new byte[100];
            manager.pushBuffer(ByteBuffer.wrap(buffer), completionListener);
        }
    });

    verify(audioStreamListener, timeout(10000)).sendAudio(any(ByteBuffer.class), any(Long.class),  eq(completionListener));
}
 
Example 12
Source File: ExcelWriterStepTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public Object answer( InvocationOnMock invocation ) throws Throwable {
  throw new RuntimeException( "This method (" + invocation.getMethod() + ") shouldn't have been called." );
}