我想在我的发票类中存根methot generateFerenceNumber():
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();
}
您不需要模拟发票
来测试它。或者,如果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。时间本地日期。now()关于如何模拟我的LocalDateTime。now()调用。我基本上遵循了所有步骤,但只使用了LocalDateTime而不是LocalDate。 我的代码的功能是这样的,它应该只在一小时的第15或45分钟运行。因此,我将LOCAL\u DATE\u TIME静态变量设置为: 然后在我的@Before测试方法中,我有以下内容: 这是2
包括StackTrace。我尝试用这个when(requestBodySpec.body(any())替换body mock。然后返回(requestHeadersSpec);也但是没有用。我认为问题在于模仿单声道。只是反对。不确定可能是什么问题。如果有不同的方法来模拟单声道。请分开反对并通过,请让我知道。
运行jUnit时的异常 我想测试这个类,下面是测试方法 运行junit会产生以下异常
我试图在中模拟axios和。到目前为止,这是我的代码: 据我所知,这和文件里的一模一样 确切的例子: 并且我得到了 我错过了什么?
问题内容: 我在带有打字稿的React Router v5.1.2中使用UseHistory挂钩吗?运行单元测试时,我遇到了问题。 TypeError:无法读取未定义的属性“ history”。 我也尝试使用,但仍然无法正常工作。 问题答案: 浅化使用的反应功能组件时,我需要相同的内容。 在我的测试文件中解决了以下模拟问题: