英文:
Getting 'Misplaced or misused argument matcher detected' on mockito tests
问题
我有以下的单元测试:
@InjectMocks
private PaymentTicketService paymentTicketService;
@Test
public void blahblahTest() {
...
String spokesmanDocumentNumber = "47246536809";
PaymentTicketPdfResponse paymentTicketPdfResponse = Fixture.from(...).gimme(...);
when(paymentTicketService.generatePaymentTicket(any(GeneratePaymentTicketRequest.class), spokesmanDocumentNumber)).thenReturn(paymentTicketPdfResponse);
...
}
测试方法如下:
public PaymentTicketPdfResponse generatePaymentTicket(GeneratePaymentTicketRequest generateBankTicketRequest, String spokesmanDocumentNumber) {
...
}
在我的测试的when
子句上,我收到一个检测到错误的错误位置或错误使用的参数匹配器
错误。它说我不能在验证或存根之外使用参数匹配器。
我真的不明白这个错误,因为我正在向模拟的方法传递any(GeneratePaymentTicketRequest.class)
和一个实际的字符串。
我在这里做错了什么?
英文:
I have the following Unit test:
@InjectMocks
private PaymentTicketService paymentTicketService;
@Test
public void blahblahTest() {
...
String spokesmanDocumentNumber = "47246536809";
PaymentTicketPdfResponse paymentTicketPdfResponse = Fixture.from(...).gimme(...);
when(paymentTicketService.generatePaymentTicket(any(GeneratePaymentTicketRequest.class), spokesmanDocumentNumber)).thenReturn(paymentTicketPdfResponse);
...
}
The testing method is this one:
public PaymentTicketPdfResponse generatePaymentTicket(GeneratePaymentTicketRequest generateBankTicketRequest, String spokesmanDocumentNumber) {
...
}
I'm getting a Misplaced or misused argument matcher detected
error on the 'when' clause of my test. It says that I cannot use argument matchers outside of verification or stubbing.
I don't really understand this error, as I'm passing any(GeneratePaymentTicketRequest.class) and a acctual string to the mocked method.
What I'm doing wrong here?
答案1
得分: 2
无法混合使用匹配器和实际值。请将值包装在匹配器中:
when(paymentTicketService.generatePaymentTicket(any(GeneratePaymentTicketRequest.class), eq(spokesmanDocumentNumber))).thenReturn(paymentTicketPdfResponse);
英文:
You can't mix matchers and actual values. Wrap the value in a matcher:
when(paymentTicketService.generatePaymentTicket(any(GeneratePaymentTicketRequest.class), eq(spokesmanDocumentNumber))).thenReturn(paymentTicketPdfResponse);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论