gpt4 book ai didi

java - 多次模拟时出现 Mockito UnfinishedStubbingException

转载 作者:行者123 更新时间:2023-12-02 08:56:57 29 4
gpt4 key购买 nike

我想在我的发票类中 stub 方法generateReferenceNumber():

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();
}
}

该对象用于我想要测试的服务中。为此,我需要多张发票。所以我写了以下代码:

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();
}

当我执行测试时,出现以下错误:

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();

有人知道为什么吗?

谢谢。

最佳答案

您不需要模拟 Invoice 来测试它。或者,如果是单元测试,您可以模拟 InvoiceReference 。如果是集成测试:

class Test {

private Invoice invoice;

@BeforeEach
void setUp() {
invoice - new Invoice();
}
@Test
void shouldSetReferenceNumber_WhenReferenceNumberEqualsNull() {
invoice.setIssueDate(new Date());
invoice.generateReferenceNumber();
assertThat(invoice).hasFieldWithValue("referenceNumber", expectingValue);
}

@Test
void shouldNotSetReferenceNumber_WhenReferenceNumberIsNotEqualsNull() {
invoice.setReferenceNumber("test-reference-number");
invoice.generateReferenceNumber();
assertThat(invoice).hasFieldWithValue("referenceNumber", "test-reference-number");
}
}

单元测试(模拟 InvoiceReference):

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;
}

getInvoiceReference().createNew(issueDate).toString();
}

public InvoiceReference getInvoiceReference() {
return new InvoiceReference();
}
}

class Test {

private Invoice invoice;
@Mock
private InvoiceReference invoiceReference;

@BeforeEach
void setUp() {
invoice = new Invoice();
MockitoAnnotations.initMocks(this);
when(invoiceReference.createNew(any(Date.class)).thenReturn("test-reference-number");
}

@Test
void shouldSetReferenceNumber_WhenReferenceNumberEqualsNull() {
invoice.setIssueDate(new Date());
invoice.generateReferenceNumber();
assertThat(invoice).hasFieldWithValue("referenceNumber", "test-reference-number");
}

@Test
void shouldNotSetReferenceNumber_WhenReferenceNumberIsNotEqualsNull() {
invoice.setReferenceNumber("different_reference_number");
invoice.generateReferenceNumber();
assertThat(invoice).hasFieldWithValue("referenceNumber", "different_reference_number");
}
}

关于java - 多次模拟时出现 Mockito UnfinishedStubbingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60440670/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com