类org.mockito.internal.stubbing.defaultanswers.ForwardsInvocations源码实例Demo

下面列出了怎么用org.mockito.internal.stubbing.defaultanswers.ForwardsInvocations的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: bullet   文件: SimpleComponentTest.java
@Before public void setUp() {
  // We cannot spy the Dagger‡ component as it's final, so we wrap it in a mock that delegates to it.
  // We want to test both that the method is called (mockito) and that everything actually works (dagger).
  SimpleComponent realComponent = DaggerSimpleComponentTest_SimpleComponent.create();
  this.component = mock(SimpleComponent.class, new ForwardsInvocations(realComponent));
  graph = new BulletSimpleComponentTest_SimpleComponent(component);
}
 
源代码2 项目: bullet   文件: MembersInjectionTest.java
@Before public void setUp() {
  // We cannot spy the Dagger‡ component as it's final, so we wrap it in a mock that delegates to it.
  // We want to test both that the method is called (mockito) and that everything actually works (dagger).
  SimpleComponent realComponent = DaggerMembersInjectionTest_SimpleComponent.create();
  this.component = mock(SimpleComponent.class, new ForwardsInvocations(realComponent));
  graph = new BulletMembersInjectionTest_SimpleComponent(component);
}
 
源代码3 项目: bullet   文件: SimpleSubcomponentTest.java
@Before public void setUp() {
  // We cannot spy the Dagger‡ component as it's final, so we wrap it in a mock that delegates to it.
  // We want to test both that the method is called (mockito) and that everything actually works (dagger).
  SimpleSubcomponent realComponent = DaggerSimpleSubcomponentTest_SimpleComponent.create().subcomponent();
  this.component = mock(SimpleSubcomponent.class, new ForwardsInvocations(realComponent));
  graph = new BulletSimpleSubcomponentTest_SimpleSubcomponent(component);
}
 
@Before
public void setup() throws IOException {

    secureRandom = mock(SecureRandom.class);
    doAnswer(new ForwardsInvocations(new Random())).when(secureRandom).nextBytes(any(byte[].class));
    tlsToolkitStandaloneCommandLine = new TlsToolkitStandaloneCommandLine(new PasswordUtil(secureRandom));
    outputFolder = tempFolder.newFolder("splitKeystoreOutputDir");
}
 
@Before
public void setup() {
    secureRandom = mock(SecureRandom.class);
    doAnswer(new ForwardsInvocations(new Random())).when(secureRandom).nextBytes(any(byte[].class));
    tlsToolkitStandaloneCommandLine = new TlsToolkitStandaloneCommandLine(new PasswordUtil(secureRandom));
}
 
源代码6 项目: astor   文件: AdditionalAnswers.java
/**
 * An answer that directly forwards the calls to the delegate.
 * <p>
 * Useful for spies or partial mocks of objects that are difficult to mock
 * or spy using the usual spy API. Possible use cases:
 * <ul>
 *     <li>Final classes but with an interface</li>
 *     <li>Already custom proxied object</li>
 *     <li>Special objects with a finalize method, i.e. to avoid executing it 2 times</li>
 * </ul>
 * For more details including the use cases reported by users take a look at
 * <a link="http://code.google.com/p/mockito/issues/detail?id=145">issue 145</a>.
 * <p>
 * The difference with the regular spy:
 * <ul>
 *   <li>
 *     The regular spy ({@link Mockito#spy(Object)}) contains <strong>all</strong> state from the spied instance
 *     and the methods are invoked on the spy. The spied instance is only used at mock creation to copy the state from.
 *     If you call a method on a regular spy and it internally calls other methods on this spy, those calls are remembered
 *     for verifications, and they can be effectively stubbed.
 *   </li>
 *   <li>
 *     The mock that delegates simply delegates all methods to the delegate.
 *     The delegate is used all the time as methods are delegated onto it.
 *     If you call a method on a mock that delegates and it internally calls other methods on this mock,
 *     those calls are <strong>not</strong> remembered for verifications, stubbing does not have effect on them, too.
 *     Mock that delegates is less powerful than the regular spy but it is useful when the regular spy cannot be created.
 *   </li>
 * </ul>
 * An example with a final class that we want to delegate to:
 * <p>
 * <pre class="code"><code class="java">
 *   final class DontYouDareToMockMe implements list { ... }
 *
 *   DontYouDareToMockMe awesomeList = new DontYouDareToMockMe();
 *
 *   List mock = mock(List.class, delegatesTo(awesomeList));
 * </code></pre>
 *
 * <p>
 * This feature suffers from the same drawback as the spy.
 * The mock will call the delegate if you use regular when().then() stubbing style.
 * Since the real implementation is called this might have some side effects.
 * Therefore you should to use the doReturn|Throw|Answer|CallRealMethod stubbing style. Example:
 *
 * <pre class="code"><code class="java">
 *   List listWithDelegate = mock(List.class, AdditionalAnswers.delegatesTo(awesomeList));
 *
 *   //Impossible: real method is called so listWithDelegate.get(0) throws IndexOutOfBoundsException (the list is yet empty)
 *   when(listWithDelegate.get(0)).thenReturn("foo");
 *
 *   //You have to use doReturn() for stubbing
 *   doReturn("foo").when(listWithDelegate).get(0);
 * </code></pre>
 *
 * @param delegate The delegate to forward calls to.
 * @return the answer
 *
 * @since 1.9.5
 */
public static <T> Answer<T> delegatesTo(Object delegate) {
    return (Answer<T>) new ForwardsInvocations(delegate);
}
 
 类所在包
 类方法
 同包方法