I'm using Mockito for unit testingand I'm getting the following exception.
org.mockito.exceptions.base.MockitoException:
`'setResponseTimeStampUtc'` is a *void method* and it *cannot* be stubbed with a *return value*!
Voids are usually stubbed with Throwables:
doThrow(exception).when(mock).someVoidMethod();
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. The method you are trying to stub is *overloaded*. Make sure you are calling the right overloaded version.
2. Somewhere in your test you are stubbing *final methods*. Sorry, Mockito does not verify/stub final methods.
3. A spy is stubbed using `when(spy.foo()).then()` syntax. It is safer to stub spies -
- with `doReturn|Throw()` family of methods. More in javadocs for Mockito.spy() method.
Here is the actual class
@Repository
public class CardRepositoryImpl implements ICardRepository {
@Autowired
private OperationBean operation;
@Override
public OperationBean getCardOperation(final String cardHolderId, final String requestTimeStamp) {
operation.setRequestTimeStampUtc(requestTimeStamp);
operation.setResponseTimeStampUtc(DateUtil.getUTCDate());
return operation;
}
}
this is the Test class I have written.
@RunWith(MockitoJUnitRunner.class)
public class CardRepositoryImplUnitTestFixture_Mockito {
@InjectMocks
private CardRepositoryImpl cardRepositoryImpl;
private OperationBean operationBean;
@Before
public void beforeTest() {
MockitoAnnotations.initMocks(this);
}
@Test
public void canGetCardOperation(){
when(cardRepositoryImpl.getCardOperation("2", Mockito.anyString())).thenReturn(operationBean);
}
}
Is this an issue with the return statement since format() method is a final method.
public static String getUTCDate() {
final TimeZone timeZone = TimeZone.getTimeZone("UTC");
final Calendar calendar = Calendar.getInstance(timeZone);
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'");
simpleDateFormat.setTimeZone(timeZone);
return simpleDateFormat.format(calendar.getTime());
}
How can I resolve this?