org.mockito.internal.stubbing.defaultanswers.ForwardsInvocations Java Examples

The following examples show how to use org.mockito.internal.stubbing.defaultanswers.ForwardsInvocations. 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: SimpleComponentTest.java    From bullet with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #2
Source File: MembersInjectionTest.java    From bullet with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #3
Source File: SimpleSubcomponentTest.java    From bullet with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #4
Source File: TlsToolkitStandaloneCommandLineTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example #5
Source File: TlsToolkitStandaloneCommandLineTest.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@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));
}
 
Example #6
Source File: AdditionalAnswers.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}