Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2007 Mockito contributors
   * This program is made available under the terms of the MIT License.
   */
  package org.mockito.stubbing;
  
Simply put: "When the x method is called then return y". E.g:
 when(mock.someMethod()).thenReturn(10);

 //you can use flexible argument matchers, e.g:
 when(mock.someMethod(anyString())).thenReturn(10);

 //setting exception to be thrown:
 when(mock.someMethod("some arg")).thenThrow(new RuntimeException());

 //you can set different behavior for consecutive method calls.
 //Last stubbing (e.g: thenReturn("foo")) determines the behavior of further consecutive calls.
 when(mock.someMethod("some arg"))
  .thenThrow(new RuntimeException())
  .thenReturn("foo");
 
 //There is a shorter way of consecutive stubbing:
 when(mock.someMethod()).thenReturn(1,2,3);
 when(mock.otherMethod()).thenThrow(exc1, exc2);
 
See examples in javadoc for org.mockito.Mockito.when(java.lang.Object)
 
 public interface OngoingStubbing<T> extends IOngoingStubbing {

    
Sets a return value to be returned when the method is called. E.g:
 when(mock.someMethod()).thenReturn(10);
 
See examples in javadoc for org.mockito.Mockito.when(java.lang.Object)

Parameters:
value return value
Returns:
iOngoingStubbing object that allows stubbing consecutive calls
 
     OngoingStubbing<T> thenReturn(T value);

    
Sets consecutive return values to be returned when the method is called. E.g:
 when(mock.someMethod()).thenReturn(1, 2, 3);
 
Last return value in the sequence (in example: 3) determines the behavior of further consecutive calls.

See examples in javadoc for org.mockito.Mockito.when(java.lang.Object)

Parameters:
value first return value
values next return values
Returns:
iOngoingStubbing object that allows stubbing consecutive calls
 
     OngoingStubbing<T> thenReturn(T value, T... values);

    
Sets Throwable objects to be thrown when the method is called. E.g:
 when(mock.someMethod()).thenThrow(new RuntimeException());
 
If throwables contain a checked exception then it has to match one of the checked exceptions of method signature.

You can specify throwables to be thrown for consecutive calls. In that case the last throwable determines the behavior of further consecutive calls.

if throwable is null then exception will be thrown.

See examples in javadoc for org.mockito.Mockito.when(java.lang.Object)

Parameters:
throwables to be thrown on method invocation
Returns:
iOngoingStubbing object that allows stubbing consecutive calls
 
     OngoingStubbing<T> thenThrow(Throwable... throwables);

    
Sets the real implementation to be called when the method is called on a mock object.

As usual you are going to read the partial mock warning: Object oriented programming is more less tackling complexity by dividing the complexity into separate, specific, SRPy objects. How does partial mock fit into this paradigm? Well, it just doesn't... Partial mock usually means that the complexity has been moved to a different method on the same object. In most cases, this is not the way you want to design your application.

However, there are rare cases when partial mocks come handy: dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.) However, I wouldn't use partial mocks for new, test-driven & well-designed code.

   // someMethod() must be safe (e.g. doesn't throw, doesn't have dependencies to the object state, etc.)
   // if it isn't safe then you will have trouble stubbing it using this api. Use Mockito.doCallRealMethod() instead. 
   when(mock.someMethod()).thenCallRealMethod();
   
   // calls real method:
   mock.someMethod();
   
 
See also javadoc org.mockito.Mockito.spy(java.lang.Object) to find out more about partial mocks. Mockito.spy() is a recommended way of creating partial mocks. The reason is it guarantees real methods are called against correctly constructed object because you're responsible for constructing the object passed to spy() method.

See examples in javadoc for org.mockito.Mockito.when(java.lang.Object)

Returns:
iOngoingStubbing object that allows stubbing consecutive calls
    OngoingStubbing<T> thenCallRealMethod();

    
Sets a generic Answer for the method. E.g:
 when(mock.someMethod(10)).thenAnswer(new Answer<Integer>() {
     public Integer answer(InvocationOnMock invocation) throws Throwable {
         return (Integer) invocation.getArguments()[0];
     }
 }
 

Parameters:
answer the custom answer to execute.
Returns:
iOngoingStubbing object that allows stubbing consecutive calls
    OngoingStubbing<T> thenAnswer(Answer<?> answer);
New to GrepCode? Check out our FAQ X