I want to stub the methot generateReferenceNumber() in my Invoice class :
public class Invoice {
private String id;
private String referenceNumber;
private Date issueDate;
private PaymentMethod paymentMethod;
private Date paymentDate;
private String shopId;
private List<InvoiceGroupItem> groupItems;
private InvoiceStatus status;
public void moveToNextState() {
status = status.nextState();
}
public void generateReferenceNumber() {
if (referenceNumber != null) {
return;
}
referenceNumber = new InvoiceReference().createNew(issueDate).toString();
}
}
This object is used in a service which I want to test. For that purpose I need multiple Invoices. So I wrote de following code :
private Stream<Invoice> buildApprovedInvoice() {
val approvedInvoices = new ArrayList<Invoice>();
for (int i = 0; i < 10; i++) {
val invoice = Invoice.builder().build();
val spy = spy(invoice);
doAnswer(invocation -> {
final Invoice mock = (Invoice) invocation.getMock();
mock.setReferenceNumber("Invoice reference number");
return null;
}).when(spy).generateReferenceNumber();
approvedInvoices.add(spy);
}
return approvedInvoices.stream();
}
And when I execute the test, I get the following error :
org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at com.coruscant.core.services.invoice.InvoiceSendingServiceTest.sendInvoices(InvoiceSendingServiceTest.java:54)
E.g. thenReturn() may be missing. Examples of correct stubbing: when(mock.isOk()).thenReturn(true); when(mock.isOk()).thenThrow(exception); doThrow(exception).when(mock).someVoidMethod();
Anyone has an idea why ?
Thank you.