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

The following examples show how to use org.mockito.invocation.InvocationOnMock#getMock() . 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: TestMerger.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private Answer<?> getKeyAnswer(final String segmentName,
    final boolean isCompressedInput) {
  return new Answer<Object>() {
    int i = 0;

    @SuppressWarnings("unchecked")
    public Boolean answer(InvocationOnMock invocation) {
      if (i++ == 3) {
        return false;
      }
      Reader<Text,Text> mock = (Reader<Text,Text>) invocation.getMock();
      int multiplier = isCompressedInput ? 100 : 1;
      mock.bytesRead += 10 * multiplier;
      Object[] args = invocation.getArguments();
      DataInputBuffer key = (DataInputBuffer) args[0];
      key.reset(("Segment Key " + segmentName + i).getBytes(), 20);
      return true;
    }
  };
}
 
Example 2
Source File: TestMerger.java    From big-c with Apache License 2.0 6 votes vote down vote up
private Answer<?> getKeyAnswer(final String segmentName,
    final boolean isCompressedInput) {
  return new Answer<Object>() {
    int i = 0;

    @SuppressWarnings("unchecked")
    public Boolean answer(InvocationOnMock invocation) {
      if (i++ == 3) {
        return false;
      }
      Reader<Text,Text> mock = (Reader<Text,Text>) invocation.getMock();
      int multiplier = isCompressedInput ? 100 : 1;
      mock.bytesRead += 10 * multiplier;
      Object[] args = invocation.getArguments();
      DataInputBuffer key = (DataInputBuffer) args[0];
      key.reset(("Segment Key " + segmentName + i).getBytes(), 20);
      return true;
    }
  };
}
 
Example 3
Source File: ReturnsCustomMocks.java    From gwtmockito with Apache License 2.0 6 votes vote down vote up
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
  // Make JavaScriptObject.cast work in most cases by forcing it to return the underlying mock
  // instead of a new mock of type JavaScriptObject. This allows cast to be used in situations
  // that don't violate the Java type system, but not in situations that do (even though
  // javascript would allow them).
  String methodName = invocation.getMethod().getName();
  if (invocation.getMock() instanceof JavaScriptObject && methodName.equals("cast")) {
    return invocation.getMock();
  } else if (invocation.getMock() instanceof Element && methodName.equals("getTagName")) {
    String className = invocation.getMock().getClass().getSimpleName();
    return className.substring(0, className.indexOf("Element")).toLowerCase();
  } else if (invocation.getMock() instanceof InputElement && methodName.equals("getType")) {
    return "text";
  } else {
    return super.answer(invocation);
  }
}
 
Example 4
Source File: ReturnsEmptyValues.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public Object answer(InvocationOnMock invocation) {
    if (methodsGuru.isToString(invocation.getMethod())) {
        Object mock = invocation.getMock();
        MockName name = new MockUtil().getMockName(mock);
        if (name.isSurrogate()) {
            return "Mock for " + ClassNameFinder.classNameForMock(mock) + ", hashCode: " + mock.hashCode();
        } else {
            return name.toString();
        }
    } else if (methodsGuru.isCompareToMethod(invocation.getMethod())) {
        //see issue 184.
        //mocks by default should not return 0 for compareTo because they are not the same. Hence we return 1 (anything but 0 is good).
        //Only for compareTo() method by the Comparable interface
        return 1;
    }
    
    Class<?> returnType = invocation.getMethod().getReturnType();
    return returnValueFor(returnType);
}
 
Example 5
Source File: ReturnsEmptyValues.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public Object answer(InvocationOnMock invocation) {
    if (methodsGuru.isToString(invocation.getMethod())) {
        Object mock = invocation.getMock();
        MockName name = mockUtil.getMockName(mock);
        if (name.isDefault()) {
            return "Mock for " + mockUtil.getMockSettings(mock).getTypeToMock().getSimpleName() + ", hashCode: " + mock.hashCode();
        } else {
            return name.toString();
        }
    } else if (methodsGuru.isCompareToMethod(invocation.getMethod())) {
        //see issue 184.
        //mocks by default should return 0 if references are the same, otherwise some other value because they are not the same. Hence we return 1 (anything but 0 is good).
        //Only for compareTo() method by the Comparable interface
        return invocation.getMock() == invocation.getArguments()[0] ? 0 : 1;
    }
    
    Class<?> returnType = invocation.getMethod().getReturnType();
    return returnValueFor(returnType);
}
 
Example 6
Source File: AnswerWithSelf.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Object answer(final InvocationOnMock invocationOnMock) throws Throwable
// CS:ON
{
    final Object mock = invocationOnMock.getMock();
    if (invocationOnMock.getMethod().getReturnType().isInstance(mock)) {
        return mock;
    }

    return RETURNS_DEFAULTS.answer(invocationOnMock);
}
 
Example 7
Source File: FluentAnswer.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
  Class<?> returnType = invocation.getMethod().getReturnType();

  Object mock = invocation.getMock();
  if (returnType.isAssignableFrom(mock.getClass())) {
    return mock;
  }
  else {
    return null;
  }
}
 
Example 8
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 9
Source File: HomieThingHandlerTests.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public Object createSubscriberAnswer(InvocationOnMock invocation) {
    final AbstractMqttAttributeClass attributes = (AbstractMqttAttributeClass) invocation.getMock();
    final ScheduledExecutorService scheduler = (ScheduledExecutorService) invocation.getArguments()[0];
    final Field field = (Field) invocation.getArguments()[1];
    final String topic = (String) invocation.getArguments()[2];
    final boolean mandatory = (boolean) invocation.getArguments()[3];
    final SubscribeFieldToMQTTtopic s = spy(
            new SubscribeFieldToMQTTtopic(scheduler, field, attributes, topic, mandatory));
    doReturn(CompletableFuture.completedFuture(true)).when(s).subscribeAndReceive(any(), anyInt());
    return s;
}
 
Example 10
Source File: MqttTopicClassMapperTests.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public Object createSubscriberAnswer(InvocationOnMock invocation) {
    final AbstractMqttAttributeClass attributes = (AbstractMqttAttributeClass) invocation.getMock();
    final ScheduledExecutorService scheduler = (ScheduledExecutorService) invocation.getArguments()[0];
    final Field field = (Field) invocation.getArguments()[1];
    final String topic = (String) invocation.getArguments()[2];
    final boolean mandatory = (boolean) invocation.getArguments()[3];
    final SubscribeFieldToMQTTtopic s = spy(
            new SubscribeFieldToMQTTtopic(scheduler, field, attributes, topic, mandatory));
    doReturn(CompletableFuture.completedFuture(true)).when(s).subscribeAndReceive(any(), anyInt());
    return s;
}
 
Example 11
Source File: HomieImplementationTests.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public Node createSpyNode(InvocationOnMock invocation) {
    final Device device = (Device) invocation.getMock();
    final String id = (String) invocation.getArguments()[0];
    // Create the node
    Node node = spy(device.createNode(id, spy(new NodeAttributes())));
    // Intercept creating a property in the next call and inject a spy'ed property.
    doAnswer(this::createSpyProperty).when(node).createProperty(any());
    return node;
}
 
Example 12
Source File: FluentAnswer.java    From camunda-bpm-mockito with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the mock itself if return type and mock type match (assume fluent
 * api).
 *
 * @param invocation
 *          the method invocation. If its return type equals the mock type,
 *          just return the mock.
 * @return the mock itself or null (meaning further stubbing is required).
 * @throws Throwable
 *           when anything fails
 */
@Override
@SuppressWarnings("unchecked")
public T answer(@Nonnull final InvocationOnMock invocation) throws Throwable {
  final Class<?> methodReturnType = invocation.getMethod().getReturnType();
  if (type.isAssignableFrom(methodReturnType) // fluent api methods
    || Query.class.isAssignableFrom(methodReturnType) // asc/desc
  ) {
    Object mock = invocation.getMock();
    return (T)mock ;
  }
  return null;
}
 
Example 13
Source File: FluentMessageCorrelationBuilderAnswer.java    From camunda-bpm-mockito with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the mock itself if return type and mock type match (assume fluent
 * api).
 *
 * @param invocation the method invocation. If its return type equals the mock type,
 *                   just return the mock.
 *
 * @return the mock itself or null (meaning further stubbing is required).
 *
 * @throws Throwable when anything fails
 */
@Override
@SuppressWarnings("unchecked")
public MessageCorrelationBuilder answer(@Nonnull final InvocationOnMock invocation) throws Throwable {
  final Class<?> methodReturnType = invocation.getMethod().getReturnType();
  if (MessageCorrelationBuilder.class.isAssignableFrom(methodReturnType)) {
    // fluent api methods
    Object mock = invocation.getMock();
    return (MessageCorrelationBuilder) mock;
  }
  return null;
}
 
Example 14
Source File: SelfReturningAnswer.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
public Object answer(InvocationOnMock invocation) throws Throwable {
  Object mock = invocation.getMock();
  if (invocation.getMethod().getReturnType().isInstance(mock)) {
    return mock;
  } else {
    return Mockito.RETURNS_DEFAULTS.answer(invocation);
  }
}
 
Example 15
Source File: MockitoAnswersUtil.java    From appengine-plugins-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
  Class<?> returnType = invocation.getMethod().getReturnType();
  if (returnType == clazz) {
    return invocation.getMock();
  } else {
    return delegate.answer(invocation);
  }
}
 
Example 16
Source File: HomieImplementationTests.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
public Property createSpyProperty(InvocationOnMock invocation) {
    final Node node = (Node) invocation.getMock();
    final String id = (String) invocation.getArguments()[0];
    Property property = spy(node.createProperty(id, spy(new PropertyAttributes())));
    return property;
}
 
Example 17
Source File: MocksSerializationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public Object answer(InvocationOnMock invocation) throws Throwable {
    invocation.getArguments();
    invocation.getMock();
    return string;
}
 
Example 18
Source File: MocksSerializationForAnnotationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public Object answer(InvocationOnMock invocation) throws Throwable {
    invocation.getArguments();
    invocation.getMock();
    return string;
}
 
Example 19
Source File: MocksSerializationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public Object answer(InvocationOnMock invocation) throws Throwable {
    invocation.getArguments();
    invocation.getMock();
    return string;
}