Java 类org.mockito.stubbing.Stubber 实例源码

项目:hadoop-oss    文件:MockitoUtil.java   
/**
 * Throw an exception from the mock/spy only in the case that the
 * call stack at the time the method has a line which matches the given
 * pattern.
 *
 * @param t the Throwable to throw
 * @param pattern the pattern against which to match the call stack trace
 * @return the stub in progress
 */
public static Stubber doThrowWhenCallStackMatches(
    final Throwable t, final String pattern) {
  return Mockito.doAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      t.setStackTrace(Thread.currentThread().getStackTrace());
      for (StackTraceElement elem : t.getStackTrace()) {
        if (elem.toString().matches(pattern)) {
          throw t;
        }
      }
      return invocation.callRealMethod();
    }
  });
}
项目:hadoop    文件:MockitoUtil.java   
/**
 * Throw an exception from the mock/spy only in the case that the
 * call stack at the time the method has a line which matches the given
 * pattern.
 *
 * @param t the Throwable to throw
 * @param pattern the pattern against which to match the call stack trace
 * @return the stub in progress
 */
public static Stubber doThrowWhenCallStackMatches(
    final Throwable t, final String pattern) {
  return Mockito.doAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      t.setStackTrace(Thread.currentThread().getStackTrace());
      for (StackTraceElement elem : t.getStackTrace()) {
        if (elem.toString().matches(pattern)) {
          throw t;
        }
      }
      return invocation.callRealMethod();
    }
  });
}
项目:aliyun-oss-hadoop-fs    文件:MockitoUtil.java   
/**
 * Throw an exception from the mock/spy only in the case that the
 * call stack at the time the method has a line which matches the given
 * pattern.
 *
 * @param t the Throwable to throw
 * @param pattern the pattern against which to match the call stack trace
 * @return the stub in progress
 */
public static Stubber doThrowWhenCallStackMatches(
    final Throwable t, final String pattern) {
  return Mockito.doAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      t.setStackTrace(Thread.currentThread().getStackTrace());
      for (StackTraceElement elem : t.getStackTrace()) {
        if (elem.toString().matches(pattern)) {
          throw t;
        }
      }
      return invocation.callRealMethod();
    }
  });
}
项目:big-c    文件:MockitoUtil.java   
/**
 * Throw an exception from the mock/spy only in the case that the
 * call stack at the time the method has a line which matches the given
 * pattern.
 *
 * @param t the Throwable to throw
 * @param pattern the pattern against which to match the call stack trace
 * @return the stub in progress
 */
public static Stubber doThrowWhenCallStackMatches(
    final Throwable t, final String pattern) {
  return Mockito.doAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      t.setStackTrace(Thread.currentThread().getStackTrace());
      for (StackTraceElement elem : t.getStackTrace()) {
        if (elem.toString().matches(pattern)) {
          throw t;
        }
      }
      return invocation.callRealMethod();
    }
  });
}
项目:hadoop-2.6.0-cdh5.4.3    文件:MockitoUtil.java   
/**
 * Throw an exception from the mock/spy only in the case that the
 * call stack at the time the method has a line which matches the given
 * pattern.
 *
 * @param t the Throwable to throw
 * @param pattern the pattern against which to match the call stack trace
 * @return the stub in progress
 */
public static Stubber doThrowWhenCallStackMatches(
    final Throwable t, final String pattern) {
  return Mockito.doAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      t.setStackTrace(Thread.currentThread().getStackTrace());
      for (StackTraceElement elem : t.getStackTrace()) {
        if (elem.toString().matches(pattern)) {
          throw t;
        }
      }
      return invocation.callRealMethod();
    }
  });
}
项目:hadoop-plus    文件:MockitoUtil.java   
/**
 * Throw an exception from the mock/spy only in the case that the
 * call stack at the time the method has a line which matches the given
 * pattern.
 *
 * @param t the Throwable to throw
 * @param pattern the pattern against which to match the call stack trace
 * @return the stub in progress
 */
public static Stubber doThrowWhenCallStackMatches(
    final Throwable t, final String pattern) {
  return Mockito.doAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      t.setStackTrace(Thread.currentThread().getStackTrace());
      for (StackTraceElement elem : t.getStackTrace()) {
        if (elem.toString().matches(pattern)) {
          throw t;
        }
      }
      return invocation.callRealMethod();
    }
  });
}
项目:hops    文件:MockitoUtil.java   
/**
 * Throw an exception from the mock/spy only in the case that the
 * call stack at the time the method has a line which matches the given
 * pattern.
 *
 * @param t the Throwable to throw
 * @param pattern the pattern against which to match the call stack trace
 * @return the stub in progress
 */
public static Stubber doThrowWhenCallStackMatches(
    final Throwable t, final String pattern) {
  return Mockito.doAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      t.setStackTrace(Thread.currentThread().getStackTrace());
      for (StackTraceElement elem : t.getStackTrace()) {
        if (elem.toString().matches(pattern)) {
          throw t;
        }
      }
      return invocation.callRealMethod();
    }
  });
}
项目:hadoop-TCP    文件:MockitoUtil.java   
/**
 * Throw an exception from the mock/spy only in the case that the
 * call stack at the time the method has a line which matches the given
 * pattern.
 *
 * @param t the Throwable to throw
 * @param pattern the pattern against which to match the call stack trace
 * @return the stub in progress
 */
public static Stubber doThrowWhenCallStackMatches(
    final Throwable t, final String pattern) {
  return Mockito.doAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      t.setStackTrace(Thread.currentThread().getStackTrace());
      for (StackTraceElement elem : t.getStackTrace()) {
        if (elem.toString().matches(pattern)) {
          throw t;
        }
      }
      return invocation.callRealMethod();
    }
  });
}
项目:hardfs    文件:MockitoUtil.java   
/**
 * Throw an exception from the mock/spy only in the case that the
 * call stack at the time the method has a line which matches the given
 * pattern.
 *
 * @param t the Throwable to throw
 * @param pattern the pattern against which to match the call stack trace
 * @return the stub in progress
 */
public static Stubber doThrowWhenCallStackMatches(
    final Throwable t, final String pattern) {
  return Mockito.doAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      t.setStackTrace(Thread.currentThread().getStackTrace());
      for (StackTraceElement elem : t.getStackTrace()) {
        if (elem.toString().matches(pattern)) {
          throw t;
        }
      }
      return invocation.callRealMethod();
    }
  });
}
项目:hadoop-on-lustre2    文件:MockitoUtil.java   
/**
 * Throw an exception from the mock/spy only in the case that the
 * call stack at the time the method has a line which matches the given
 * pattern.
 *
 * @param t the Throwable to throw
 * @param pattern the pattern against which to match the call stack trace
 * @return the stub in progress
 */
public static Stubber doThrowWhenCallStackMatches(
    final Throwable t, final String pattern) {
  return Mockito.doAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
      t.setStackTrace(Thread.currentThread().getStackTrace());
      for (StackTraceElement elem : t.getStackTrace()) {
        if (elem.toString().matches(pattern)) {
          throw t;
        }
      }
      return invocation.callRealMethod();
    }
  });
}
项目:ui-automation    文件:BaseAutomationTest.java   
public static Stubber answerStringByReference(String value) {
    return doAnswer((Answer<Integer>) invocation -> {

           Object[] args = invocation.getArguments();
           PointerByReference reference = (PointerByReference)args[0];

           Pointer pointer = new Memory(Native.WCHAR_SIZE * (value.length() +1));
           pointer.setWideString(0, value);

           reference.setValue(pointer);

           return 0;
       });
}
项目:ui-automation    文件:BaseAutomationTest.java   
public static Stubber answerIntByReference(int value) {
    return doAnswer((Answer<Integer>) invocation -> {

           Object[] args = invocation.getArguments();
           IntByReference reference = (IntByReference)args[0];

           reference.setValue(value);

           return 0;
       });
}
项目:emodb    文件:DataStoreJerseyTest.java   
private Stubber doUpdateInto(final List<Update> updates) {
    return doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation)
                throws Throwable {
            //noinspection unchecked
            Iterables.addAll(updates, (Iterable<Update>) invocation.getArguments()[0]);
            return null;
        }
    });
}
项目:emodb    文件:DataStoreJerseyTest.java   
private Stubber captureUpdatesAndTags(final List<Update> updates, final Set<String> tags) {
    return doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation)
                throws Throwable {
            //noinspection unchecked
            Iterables.addAll(updates, (Iterable<Update>) invocation.getArguments()[0]);
            //noinspection unchecked
            tags.addAll((Collection<? extends String>) invocation.getArguments()[1]);
            return null;
        }
    });
}
项目:securemock    文件:Mockito.java   
public static Stubber doThrow(final Throwable toBeThrown) {
    return AccessController.doPrivileged(new PrivilegedAction<Stubber>() {
        @Override
        public Stubber run() {
            return org.mockito.Mockito.doThrow(toBeThrown);
        }
    });
}
项目:securemock    文件:Mockito.java   
public static Stubber doThrow(final Class<? extends Throwable> toBeThrown) {
    return AccessController.doPrivileged(new PrivilegedAction<Stubber>() {
        @Override
        public Stubber run() {
            return org.mockito.Mockito.doThrow(toBeThrown);
        }
    });
}
项目:securemock    文件:Mockito.java   
public static Stubber doCallRealMethod() {
    return AccessController.doPrivileged(new PrivilegedAction<Stubber>() {
        @Override
        public Stubber run() {
            return org.mockito.Mockito.doCallRealMethod();
        }
    });
}
项目:securemock    文件:Mockito.java   
public static Stubber doAnswer(final Answer answer) {
    return AccessController.doPrivileged(new PrivilegedAction<Stubber>() {
        @Override
        public Stubber run() {
            return org.mockito.Mockito.doAnswer(answer);
        }
    });
}
项目:securemock    文件:Mockito.java   
public static Stubber doNothing() {
    return AccessController.doPrivileged(new PrivilegedAction<Stubber>() {
        @Override
        public Stubber run() {
            return org.mockito.Mockito.doNothing();
        }
    });
}
项目:securemock    文件:Mockito.java   
public static Stubber doReturn(final Object toBeReturned) {
    return AccessController.doPrivileged(new PrivilegedAction<Stubber>() {
        @Override
        public Stubber run() {
            return org.mockito.Mockito.doReturn(toBeReturned);
        }
    });
}
项目:VirtualSlideViewer    文件:TestUtil.java   
public static Stubber copyToParameter(byte[] result)
{
    return Mockito.doAnswer((x) ->
    {
        byte[] dst = (byte[])x.getArguments()[0];

        System.arraycopy(result, 0, dst, 0, result.length);
        return null;
    });
}
项目:evosuite    文件:MockitoExtension.java   
/**
 * Extend Mockito API by allowing an arbitrary number of inputs to
 * the method {@code doReturn()}
 *
 * @param value
 * @param values
 * @return
 */
public static Stubber doReturn(Object value, Object... values){

    Stubber stubber = Mockito.doReturn(value);
    for(Object v : values){
        stubber = stubber.doReturn(v);
    }

    return stubber;
}
项目:lambda-behave    文件:Expect.java   
@SuppressWarnings("unchecked")
public <T> Stubber toAnswer(final Runnable method) {
    return Mockito.doAnswer(invocation -> {
        Object[] arguments = invocation.getArguments();
        method.run();
        return null;
    });
}
项目:lambda-behave    文件:Expect.java   
private  Stubber doAnswer(final Consumer<Object[]> method, final int argumentCount) {
    return Mockito.doAnswer(invocation -> {
        Object[] arguments = invocation.getArguments();
        if (arguments.length >= argumentCount) {
            method.accept(arguments);
        } else {
            failure("Invocation requires at least " + argumentCount + " argument");
        }
        return null;
    });
}
项目:papaya    文件:SpyRef.java   
public Stubber spyReturnValue() {
    return Mockito.doAnswer(new Answer<T>() {
        public T answer(InvocationOnMock invocation) throws Throwable {
            return setupSpy(invocation.callRealMethod());
        }
    });
}
项目:papaya    文件:SpyRef.java   
public Stubber spyParameter(final int parameterIdx) {
    return Mockito.doAnswer(new Answer() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            arguments[parameterIdx] = setupSpy(arguments[parameterIdx]);
            return invocation.callRealMethod();
        }
    });
}
项目:hadoop    文件:TestQuorumJournalManager.java   
private Stubber injectIOE() {
  return futureThrows(new IOException("Injected"));
}
项目:hadoop    文件:TestQuorumJournalManagerUnit.java   
static <V> Stubber futureReturns(V value) {
  ListenableFuture<V> ret = Futures.immediateFuture(value);
  return Mockito.doReturn(ret);
}
项目:hadoop    文件:TestQuorumJournalManagerUnit.java   
static Stubber futureThrows(Throwable t) {
  ListenableFuture<?> ret = Futures.immediateFailedFuture(t);
  return Mockito.doReturn(ret);
}
项目:aliyun-oss-hadoop-fs    文件:TestQuorumJournalManager.java   
private Stubber injectIOE() {
  return futureThrows(new IOException("Injected"));
}
项目:aliyun-oss-hadoop-fs    文件:TestQuorumJournalManagerUnit.java   
static <V> Stubber futureReturns(V value) {
  ListenableFuture<V> ret = Futures.immediateFuture(value);
  return Mockito.doReturn(ret);
}
项目:aliyun-oss-hadoop-fs    文件:TestQuorumJournalManagerUnit.java   
static Stubber futureThrows(Throwable t) {
  ListenableFuture<?> ret = Futures.immediateFailedFuture(t);
  return Mockito.doReturn(ret);
}
项目:big-c    文件:TestQuorumJournalManager.java   
private Stubber injectIOE() {
  return futureThrows(new IOException("Injected"));
}
项目:big-c    文件:TestQuorumJournalManagerUnit.java   
static <V> Stubber futureReturns(V value) {
  ListenableFuture<V> ret = Futures.immediateFuture(value);
  return Mockito.doReturn(ret);
}
项目:big-c    文件:TestQuorumJournalManagerUnit.java   
static Stubber futureThrows(Throwable t) {
  ListenableFuture<?> ret = Futures.immediateFailedFuture(t);
  return Mockito.doReturn(ret);
}
项目:hadoop-2.6.0-cdh5.4.3    文件:TestQuorumJournalManager.java   
private Stubber injectIOE() {
  return futureThrows(new IOException("Injected"));
}
项目:hadoop-2.6.0-cdh5.4.3    文件:TestQuorumJournalManagerUnit.java   
static <V> Stubber futureReturns(V value) {
  ListenableFuture<V> ret = Futures.immediateFuture(value);
  return Mockito.doReturn(ret);
}
项目:hadoop-2.6.0-cdh5.4.3    文件:TestQuorumJournalManagerUnit.java   
static Stubber futureThrows(Throwable t) {
  ListenableFuture<?> ret = Futures.immediateFailedFuture(t);
  return Mockito.doReturn(ret);
}
项目:hadoop-EAR    文件:TestQuorumJournalManager.java   
private Stubber injectIOE() {
  return futureThrows(new IOException("Injected"));
}
项目:hadoop-EAR    文件:TestQuorumJournalManagerUnit.java   
static <V> Stubber futureReturns(V value) {
  ListenableFuture<V> ret = Futures.immediateFuture(value);
  return Mockito.doReturn(ret);
}